【问题标题】:Wrong Cell Selected in CutomTableViewCell在自定义 TableView 单元格中选择了错误的单元格
【发布时间】:2013-10-17 15:07:17
【问题描述】:

在多选表格视图单元格中。当我选择第 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--;
}

我正在使用自定义表格视图单元格,我也检查了它的高度。我的 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;
}

【问题讨论】:

  • 从这个链接查看我的答案stackoverflow.com/questions/15702242/…
  • 使用上面链接中的我的答案,否则如果您没有大量数据,那么只需将dequeueReusableCellWithIdentifier 设置为nil 就像这样VehicleListCell *cell = (VehicleListCell *)[mytableview dequeueReusableCellWithIdentifier:nil];
  • @ParasJoshi。我试过了,但它不起作用。此外,所有 tableview 单元格都显示为空白。

标签: iphone ios objective-c


【解决方案1】:
[selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
[selectedIndexes insertObject:[NSNumber numberWithInt:indexPath.row] atIndex:counter];

在这里您添加了两次相同的对象。这可能会导致您的问题。 登录selectedIndexes 看看发生了什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多