我有一個允許多個選擇的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