0

I am aware that there are many similar questions here, but none solved my issue. There are many sections in my UITableView and when I click / checkmark some buttons in the cells and scroll down, the check mark disappears when scrolled up again. Here's what I coded..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"CheckBoxedCell";
   // NSString *cellId = [NSString stringWithFormat:@"Section:%d Row:%d",indexPath.section,indexPath.row];
    CheckBoxedCellClass *cell = (CheckBoxedCellClass *)[self.tableViewContact dequeueReusableCellWithIdentifier:cellId];

    if(!cell)
    {
        NSArray *nib;
        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        {
            nib = [[NSBundle mainBundle] loadNibNamed:@"CheckBoxedCellClass" owner:self options:nil];
        }
        else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {
            nib = [[NSBundle mainBundle] loadNibNamed:@"CheckBoxedCellClass_iPad" owner:self options:nil];
        }
            for (id object in nib)
            {
                if([object isKindOfClass:[CheckBoxedCellClass class]])
                {
                    cell = (CheckBoxedCellClass *)object;
                    break;
                }
            }

            cell = [nib objectAtIndex:0];

     }

        SaveCheckBoxedView *saveContact;
        if(isFiltered == YES)
        {
            saveContact = [filterdArray objectAtIndex:indexPath.row];
            cell.nameLabel.text = saveContact.nameString;
        }
        else
        {
            saveContact = [mutableArray objectAtIndex:indexPath.row];
            cell.nameLabel.text = [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
        }


        cell.companyLabel.text = saveContact.companyString;
        cell.invIdLabel.text = [NSString stringWithFormat:@"%@", saveContact.invitId];

        //set fonts
        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        {

            [cell.companyLabel setFont:[UIFont italicSystemFontOfSize:10.0]];
        }
        else
        {

            [cell.companyLabel setFont:[UIFont italicSystemFontOfSize:14.0]];
        }



    //handling check box

    NSInteger rowNumber = 0;
    for(NSInteger i = 0; i < indexPath.section ; i++)
    {
        rowNumber += [self tableView:self.tableViewContact numberOfRowsInSection:i];
    }

    rowNumber += indexPath.row;


    UIButton *checkBox;
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        checkBox = [[UIButton alloc]initWithFrame:CGRectMake(7, 8, 30, 30)];
    }
    else
    {
        checkBox = [[UIButton alloc]initWithFrame:CGRectMake(15, 13, 30, 30)];
    }

    [checkBox setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
    [checkBox addTarget:self action:@selector(checkBoxClicked:event:) forControlEvents:UIControlEventTouchUpInside];

    checkBox.tag = rowNumber;

    [cell.contentView addSubview:checkBox];

    // handle check box view reset when scrolled

    for(NSIndexPath *path in checkedIndexPaths)
    {
        if(path.section == indexPath.section && path.row == indexPath.row)
        {
            if(!checkedIndexPaths)
            {
                checkedIndexPaths = [[NSMutableSet alloc] init];
                [checkedIndexPaths addObject:[NSNumber numberWithInt:rowNumber]];
            }
        }
    }

    return cell;
}


-(void)checkBoxClicked:(id)sender event:(id)event
{

    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableViewContact];
    NSIndexPath *indexPath = [self.tableViewContact indexPathForRowAtPoint: currentTouchPosition];
    NSLog(@"value of indexPath.section %d ,indexPath.row %d",indexPath.section,indexPath.row);

    UIButton *tappedButton = (UIButton*)sender;
    NSLog(@"Tag number = %d", [sender tag]);


    if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"checkBox.png"]])
    {
        [sender  setImage:[UIImage imageNamed: @"checkBoxMarked.png"] forState:UIControlStateNormal];
        if(isFiltered == YES)
        {
            NSString *addId = [filteredArrayOfIds objectAtIndex:indexPath.row];
            NSLog(@"filterd id = %@", addId); //get filtered array here
            [arrayOfIds addObject:addId];
        }
        else
        {
            NSString *finalIntId = [mutableArrayOfIds objectAtIndex:tappedButton.tag];
            NSLog(@"Tagged checked button id = %@", finalIntId);
            [arrayOfIds addObject:finalIntId];
        }

    }
    else
    {
        [sender setImage:[UIImage imageNamed:@"checkBox.png"]forState:UIControlStateNormal];
        NSLog(@"UnChecked");

        [arrayOfIds removeObjectAtIndex:tappedButton.tag];
    }

}
Deepak Thakur
  • 3,453
  • 2
  • 37
  • 65

3 Answers3

0

If I am not mistaken, you are never setting checkBox.checked?

Stefan Fisk
  • 1,563
  • 13
  • 19
0

When you scroll up and down only the visible cells will be dequeued and configured. So whenever you scroll down a whole page and then up again the cells will be recreated (you can NSLog at the entry point of cellForRowAtIndexPath to see what I mean).

In this method you recreate the button. Remember that table view cells are cached so if you created a cell once it will probably be used again when needed so create the button only if the cell is created for the first time and not on every call to cellForRowAtIndexPath.

If you use a custom cell you can add a boolean member to indicate that the cell was already created and there is no need to add another newly created button which will hide you original one when you add it with [cell.contentView addSubview:checkBox]; (or better yet do the button creation in this custom cell's init method which will be more easier and correct)

giorashc
  • 13,691
  • 3
  • 35
  • 71
0

I used to implement similar things with button. So inside the cell, you define a function which changes the background of the checkbox, or the status of the checkbox based on the model.

-(void) setModel:(Model *)model {

    [self.button setSomeStyleBasedOnYourModel];        
}

If you have that checkbox in your tableview of your storyboard, you can linked a target from the storyboard to you controller.

- (IBAction)doTaskButtonPressed:(UIButton *)sender {
    [super doTaskButtonPressed:sender];
}
Ethan Fang
  • 1,271
  • 10
  • 15