user3244372
user3244372

Reputation: 3

Retain button state in TableView cell when scrolled in iOS

I am aware that there are many similar questions here, but I did not find a solution. I have a check box in UITableViewCell.

When I check and scroll down and scroll up again, its state is reset i.e. unchecked.

I am aware the cell is deque'd in cellForRowAtIndexPath, but how will I retain its state when checked?

Here's what I tried:

@interface ContactCheckedViewController ()
{
    UIButton *checkBox;
}

- (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) //Handling UISearchbar
        {
            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];
    //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];


    // handle check box view reset when scrolled


    BOOL buttonPressed = [[self.boolArray objectAtIndex:rowNumber] boolValue];
    [checkBox setSelected:buttonPressed];
    if(buttonPressed)
    {
        [checkBox setImage:[UIImage imageNamed:@"checkBoxMarked.png"] forState:UIControlStateNormal];
    }
    else
    {
        [checkBox setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
    }

    checkBox.tag = rowNumber;
    [cell.contentView addSubview:checkBox];

    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];
        NSUserDefaults *buttonDefault = [NSUserDefaults standardUserDefaults];
        [buttonDefault setBool:YES forKey:@"CHECKMARKEDKEY"];

        //add values in boolArray to retain button state 
        [boolArray replaceObjectAtIndex:[sender tag] withObject:[NSNumber numberWithBool:YES]];

        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");
        NSUserDefaults *buttonDefault = [NSUserDefaults standardUserDefaults];
        [buttonDefault setBool:NO forKey:@"CHECKMARKEDKEY"];

        [boolArray replaceObjectAtIndex:[sender tag] withObject:[NSNumber numberWithBool:NO]];

        if(isFiltered == YES)
        {
            [arrayOfIds removeObjectIdenticalTo:[filteredArrayOfIds objectAtIndex:tappedButton.tag]];
        }
        else
        {
            [arrayOfIds removeObjectIdenticalTo:[mutableArrayOfIds objectAtIndex:tappedButton.tag]];
        }
    }
    NSLog(@"bool array = %@", boolArray);

}

Please let me know what shall I change in my code. Any help with a sample code will be appreciated.

Thanks.

Upvotes: 0

Views: 1108

Answers (3)

user3651677
user3651677

Reputation: 69

static NSString *cellId = @"CheckBoxedCell";

change CheckBoxedCell to your cell class name.

I hope its work fine

Upvotes: 0

Akhilrajtr
Akhilrajtr

Reputation: 5182

Try this,

insted of boolArray use a NSMutableDictionary *boolDict

//init this in viewDidLoad
NSMutableDictionary *boolDict = [[NSMutableDictionary alloc] init];

in cellForRowAtIndexPath : use

BOOL buttonPressed = [[boolDict objectForKey:[NSString stringWithFormat:@"%d", rowNumber]] boolValue];

in checkBoxClicked : use

[boolDict setObject:[NSNumber numberWithBool:YES] forKey:[NSString stringWithFormat:@"%d", [sender tag]]];

[boolDict setObject:[NSNumber numberWithBool:NO] forKey:[NSString stringWithFormat:@"%d", [sender tag]]];

Upvotes: 0

GSD
GSD

Reputation: 436

For this you have to maintain the NSMutableArray and boolean. Let us suppose its

NSMutableArray *selectedRows; BOOL isRowSelected;

Then in 'didSelectRowAtIndexPath' you have to make a bool value 'YES' and 'NO'

UIButton *btn = (UIButton *)[cell viewWithTag://your button tag]; //your button instance

if(isRowSelected){

    // set button checked
    isRowSelected = NO;
    [selectedRows addObject:indexPath];
}
else{

    // set button checked
    isRowSelected = YES;
    [selectedRows removeObject:indexPath];
}

[tableView deselectRowAtIndexPath:indexPath animated:YES];`

Finally in 'cellForRowAtIndexPath'

// Mentain state for UIButton.
if([selectedRows containsObject:indexPath])
    // your button state : checked
else
    // your button state : unchecked

Upvotes: 1

Related Questions