简体   繁体   中英

saving the state of UITableviewcell accessoryView

I have a checkbox as an accessoryView in my UITableViewCell. When I click on the checkbox, it gets checkmarked and vise versa. But When i scroll down out of view and scroll back, the checkbox is gone. I am wondering what are some ways to save the state of my button?

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;

    if([tableView isEqual:self.myTableView]){
        static NSString *TableViewIdentifier = @"MyCells";
        cell = [tableView dequeueReusableCellWithIdentifier:TableViewIdentifier];
        if(cell == nil){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                         reuseIdentifier:TableViewIdentifier];
        } 
        //configure the cell
        checkbox = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect checkboxRect = CGRectMake(135, 150, 36, 36);
        [checkbox setFrame:checkboxRect];  
        [checkbox setImage:[UIImage imageNamed:@"unselected@2x.png"]forState:UIControlStateNormal];
        [checkbox setImage:[UIImage imageNamed:@"selected@2x.png"] forState:UIControlStateSelected];

        /*if (checkbox.state == UIControlStateNormal) {
            [checkbox setImage:[UIImage imageNamed:@"unselected@2x.png"]forState:UIControlStateHighlighted];
        }else if(checkbox.state == UIControlStateSelected){
            [checkbox setImage:[UIImage imageNamed:@"selected@2x.png"]forState:UIControlStateHighlighted];
        }*/

        [checkbox addTarget:self action:@selector(checkboxClicked:) forControlEvents:UIControlEventTouchUpInside];
        cell.accessoryView = checkbox;


        NSString *group;
        NSUInteger row = [indexPath row];

        switch (indexPath.section) {
            case 0:
                group = [suggestedPeople objectAtIndex:row];
                cell.textLabel.text = group;
                break;
            case 1:
                group = [aArray objectAtIndex:row];
                cell.textLabel.text = group;
                break;
            case 2:
                group = [bArray objectAtIndex:row];
                cell.textLabel.text = group;
                break;
            case 3:
                group = [cArray objectAtIndex:row];
                cell.textLabel.text = group;
                break;
            case 4:
                group = [dArray objectAtIndex:row];
                cell.textLabel.text = group;
                break;
            case 5:
                group = [eArray objectAtIndex:row];
                cell.textLabel.text = group;
                break;
        }
    }
    return cell;
}

EDIT:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
    checkbox = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect checkboxRect = CGRectMake(135, 150, 36, 36);
    [checkbox setFrame:checkboxRect];  
    [checkbox setImage:[UIImage imageNamed:@"unselected@2x.png"]forState:UIControlStateNormal];
    [checkbox setImage:[UIImage imageNamed:@"selected@2x.png"] forState:UIControlStateSelected];

}
return self;}

Thanks for posting the code. Right, your problem is that you are creating UIButton instances in the cellForRowAtIndexPath method. This isn't allowed (or rather, odd things will happen as you've noticed). You should create a TableViewCell subclass and add the button there, or create a XIB for the table cell and load that.

Check out this post for some help with this:

http://forums.macrumors.com/showthread.php?t=545061

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM