选择一个UITableViewCell会在滚动后选择另一个单元格

时间:2015-09-29 11:58:01

标签: ios objective-c uitableview

尽管这是一个常见的问题,但我还没有得到如何解决这个问题。

我提到Selecting one UITableViewCell selects the other cells at that same position when you scroll up or down但我不明白。

我有 allMumbaiArray (在cellForRowAtIndexPath中使用)和tableview alllMumbaiTableview 。通过使用以下代码,我可以选择并取消选择该行。

按照EI Captain的解决方案更新

 @property (nonatomic, strong) NSMutableArray *arrIndexpaths;

tableView方法 - :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];

      if (cell == nil)
      {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CellIdentifier"];
      }

      cell.textLabel.text = [allMumbaiArray objectAtIndex:indexPath.row];

    if ([self.arrIndexpaths containsObject:[NSNumber numberWithInt:indexPath.row]])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }


      return cell;

  }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.view endEditing:YES];

    NSString *testing =[allMumbaiArray objectAtIndex:indexPath.row];
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [self.arrIndexpaths addObject:[NSNumber numberWithInt:indexPath.row]];
    NSLog(@"%@",testing);
}

 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
 {
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
    tableViewCell.accessoryType = UITableViewCellAccessoryNone;
     [self.arrIndexpaths removeObject:[NSNumber numberWithInt:indexPath.row]];

 }

问题: 1)当我在滚动tablewview后选择第一行时,另一行被选中。如何修复此问题?

2)连续选择和取消选择行后,如何维护唯一选定的行值?

2 个答案:

答案 0 :(得分:2)

这是解决方案

制作一个可变数组,用于存储tableview单元格的选定索引。

@property (nonatomic, strong) NSMutableArray *arrIndexpaths;  

<强>的cellForRowAtIndexPath

if ([self.arrIndexpaths containsObject:[NSNumber numberWithLong: indexPath.row]])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}

<强> didSelectRowAtIndexPath方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {   

    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
    tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
    [self.arrIndexpaths addObject:[NSIndexSet indexSetWithIndex:indexPath.row]];

  }   

<强> didDeselectRowAtIndexPath

 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
  {

    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
    tableViewCell.accessoryType = UITableViewCellAccessoryNone;
    [self.arrIndexpaths removeObject:[NSIndexSet indexSetWithIndex:indexPath.row]];

  }

以下是示例项目......

demo project

答案 1 :(得分:0)

如果你的手机没有显示配件,那么你只需要重装它们

[_ableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

didSelectRowAtIndexPathdidDeselectRowAtIndexPath中尝试此操作。

相关问题