我有一个允许多个选择的UITableView,但由于某种原因,当我滚动UITableView时,我用UITableViewCellAccessoryCheckmark作出的选择重复,因为我滚动。UITableViewCell accessoryType 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];
}
}
任何帮助,将不胜感激的代码。
“但是出于某种原因......”的原因是小区重用。搜索“复选标记重复”,你会发现很多答案。 – rdelmar 2014-10-20 20:50:38