UITableViewCellにaccessoryViewとしてチェックボックスがあります。チェックボックスをクリックすると、チェックマークが付けられ、その逆も同様です。しかし、下にスクロールして表示を外し、後ろにスクロールすると、チェックボックスが消えます。ボタンの状態を保存する方法は何ですか?
-(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;
}
編集:
- (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;}