はい、あなたは新しいを宣言する必要があなたのデータソース数のNSMutableArray
(たとえば_selectedList
) viewDidLoad
又はinit
方法、
_selectedList = [[NSMutableArray alloc] init];
for(int i = 0; i < [datasource count]; i++)
{
[_selectedList addObject:[NSNumber numberWithBool:NO]];
}
において値0
(クラスメンバーとして).hファイルで宣言NSMutableArray *_selectedList;
とのNSNumberを移入し、次のように以下の方法を作ります。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//...
if (! [[_selectedList objectAtIndex:indexPath.row] boolValue]) {
//Deselected
cell.accessoryType = UITableViewCellAccessoryNone;
cell.backgroundColor=[UIColor clearColor];
} else {
//Selected
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.backgroundColor=[UIColor redColor];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
//Deselect
cell.accessoryType = UITableViewCellAccessoryNone;
cell.backgroundColor=[UIColor clearColor];
} else {
//Select
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.backgroundColor=[UIColor redColor];
}
BOOL isSelected = ![[_selectedList objectAtIndex:indexPath.row] boolValue];
[_selectedList replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:isSelected]];
}
それは動作します!ありがとう! –