是的,你要声明一个新的NSMutableArray
(比如_selectedList
)的数据源数量。用的NSNumber与值0
填充它在h文件(如类成员)
在viewDidLoad
或init
方法声明NSMutableArray *_selectedList;
,
_selectedList = [[NSMutableArray alloc] init];
for(int i = 0; i < [datasource count]; i++)
{
[_selectedList addObject:[NSNumber numberWithBool:NO]];
}
,并进行以下方法如下所述。
- (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]];
}
它的工作原理!谢谢! – 2012-01-12 11:26:09