在多选表格视图单元格中。当我选择第 1 个单元格时,第 7 个单元格也会自动被选中。我已经实现了多个单元格选择功能。我尝试了一些方法,但没有奏效。
这是我的代码,我做错了什么?
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
[mytableview deselectRowAtIndexPath:indexPath animated:YES];
if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
[def setObject:[model objectAtIndex:indexPath.row] forKey:[NSString stringWithFormat:@"model_%i",counter]];
[def setObject:[year objectAtIndex:indexPath.row] forKey:[NSString stringWithFormat:@"year_%i",counter]];
[def setObject:[colorid objectAtIndex:indexPath.row] forKey:[NSString stringWithFormat:@"colorid_%i",counter]];
[def setObject:[vin objectAtIndex:indexPath.row] forKey:[NSString stringWithFormat:@"vin_%i",counter]];
[def setObject:[price objectAtIndex:indexPath.row] forKey:[NSString stringWithFormat:@"price_%i",counter]];
[def setObject:[color objectAtIndex:indexPath.row] forKey:[NSString stringWithFormat:@"color_%i",counter]];
counter++;
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
[selectedIndexes insertObject:[NSNumber numberWithInt:indexPath.row] atIndex:counter];
}
else
{
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithFormat:@"model_%i",counter]];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithFormat:@"year_%i",counter]];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithFormat:@"colorid_%i",counter]];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithFormat:@"vin_%i",counter]];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithFormat:@"price_%i",counter]];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithFormat:@"color_%i",counter]];
counter--;
}
我正在使用自定义 tableview 单元格,我也检查了它的高度。我的 cellforRowAtIndexPath 方法看起来像,
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"CustomCell17";
VehicleListCell *cell = (VehicleListCell *)[mytableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"VehicleListView" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
return cell;
}