我有一个UITableView允许多个选项,但是由于某种原因,当我滚动UITableView时,我用UITableViewCellAccessoryCheckmark做的选择在我滚动时重复。
这是我正在使用的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor clearColor];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = [sortedMachineNames objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - Table view delegate
// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[selectedMachinesMArray addObject:cell.textLabel.text];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
NSUInteger index = [selectedMachinesMArray indexOfObject:cell.textLabel.text];
if (index!=NSNotFound) {
[selectedMachinesMArray removeObjectAtIndex:index];
}
}
答案 0 :(得分:2)
在cellforIndexAtPath中检查单元格的当前状态并更改附件类型的值,如下所示:
if(marked)
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;