选择一个 UITableViewCell 滚动后选择另一个单元格 [英] Selecting one UITableViewCell selects the other cell after scrolling

查看:23
本文介绍了选择一个 UITableViewCell 滚动后选择另一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管这是一个常见问题,但我不知道如何解决这个问题.

Even though it's common question but I'm not getting how to fix this.

我提到了 选择一个 UITableViewCell向上或向下滚动时选择同一位置的其他单元格但我不明白.

I referred Selecting one UITableViewCell selects the other cells at that same position when you scroll up or down But i didn't understand.

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

I'm having allMumbaiArray (using in cellForRowAtIndexPath) and tableview alllMumbaiTableview. By using following code i can select and deselect the row.

根据 EI 船长的解决方案更新

 @property (nonatomic, strong) NSMutableArray *arrIndexpaths;

tableView 方法 -:

tableView Methods -:

- (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 后选择第一行时,另一行被选中.如何解决这个问题?

Issue : 1) when i select first row after scrolling the tablewview another row get selected.How to Fix this?

2) 连续选择和取消选择行后,如何保持唯一选择的行值?

2) After selecting and deselecting row continuously, how can i maintain the only selected row values ?

推荐答案

这里是解决方案

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

make one mutable array that stores selected index of tableview cell..

@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]];

  }

这是示例项目...

演示项目

这篇关于选择一个 UITableViewCell 滚动后选择另一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆