student
student

Reputation: 179

Weird behaviour with tableview and buttons

I am trying to create a custom questionnaire. I have multiple NSMutableArray(btnAr) where I store the groups of buttons. To keep things in order, I use another NSMutableArray(radioGroupBtns) where I store all the NSMutableArrays(btnAr).

I use tableview to display the buttons. The problem I am facing is this:

When I click on a button in the first group, after scrolling to the last group the same button is clicked also.

The right image is when I scrolled at the bottom.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}


-(UITableViewCell* )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FeedbackViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

    NSInteger rowNum = [indexPath row];

    NSLog(@"RowNumb: %ld",rowNum);

    NSMutableArray *btns = [radioGroupBtns objectAtIndex:rowNum];
    NSMutableArray *lbls = [radioGroupLabels objectAtIndex:rowNum];

    for (int i=0; i<[btns count]; i++)
    {
        [cell addSubview:[btns objectAtIndex:i]];
        [cell addSubview:[lbls objectAtIndex:i]];
    }


    return cell;
}

-(void) getData
{
    int totalSections = 5;


    for (int j=0; j<totalSections; j++)
    {
        NSMutableArray *btnAr = [[NSMutableArray alloc]init];
        NSMutableArray *lblAr = [[NSMutableArray alloc]init];

        int numOfQuestions = 4;

        for (int i=0; i<numOfQuestions; i++)
        {
            RadioUIButton *button = [self getButton:i];

            button.sectionTag = j;
            button.stringTag = [NSString stringWithFormat:@"%d%d", i,button.sectionTag];
            [button setTag:i];

            UILabel *lbl = [self getLabel:i text:@"Question"];
            [lblAr addObject:lbl];
            [btnAr addObject:button];
        }

        [radioGroupBtns addObject:btnAr];
        [radioGroupLabels addObject:lblAr];
    }
}

- (void) btnPressed:(id) b
{
    RadioUIButton *btn = (RadioUIButton *) b;

    NSMutableArray *group = [radioGroupBtns objectAtIndex:btn.sectionTag];

    for (RadioUIButton *bn in group)
    {
        if ([bn tag] != [b tag])
        {
            if ([bn isSelected])
               [bn setSelected:!bn.selected];
        }
    }

    if (![btn isSelected])
        [btn setSelected:!btn.selected];

    NSLog(@"Button %ld clicked. Row: %d , String: %@", (long int)[b tag], btn.sectionTag, btn.stringTag);
}

- (RadioUIButton*) getButton:(int) i {
    RadioUIButton *button = [RadioUIButton buttonWithType:UIButtonTypeCustom];
    int hpos = i*30 + 30;
    button.frame = CGRectMake(30.0f, hpos, 30.0f, 30.0f);
    [button addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];

    //[button setTitle:@"Option A" forState:UIControlStateNormal];
    [button setImage: [UIImage imageNamed:@"radio_button_off.png"]forState:UIControlStateNormal];
    [button setImage: [UIImage imageNamed:@"radio_button_on.png"]forState: UIControlStateSelected];

    return button;
}

- (UILabel*) getLabel:(int) i text:(NSString *) txt{
    int hpos = i*30 + 30;
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(80,hpos,200,30);
    label.text = txt;

    return label;


}

Upvotes: 1

Views: 62

Answers (1)

akhiljayaram
akhiljayaram

Reputation: 141

Table view reuses cells. There are ways to handle this. But, in your case you dont seem to need to reuse cells. So use scroll view instead and inflate views inside that. And as a suggestion, try to store model objects instead of buttons. Manipulate views and their state based on the data model.

Upvotes: 1

Related Questions