Reputation: 155
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:@"[email protected]"]forState:UIControlStateNormal];
[checkbox setImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateSelected];
/*if (checkbox.state == UIControlStateNormal) {
[checkbox setImage:[UIImage imageNamed:@"[email protected]"]forState:UIControlStateHighlighted];
}else if(checkbox.state == UIControlStateSelected){
[checkbox setImage:[UIImage imageNamed:@"[email protected]"]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:@"[email protected]"]forState:UIControlStateNormal];
[checkbox setImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateSelected];
}
return self;}
Upvotes: 0
Views: 402
Reputation: 82209
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
Upvotes: 1