【问题标题】:UITableView: Keep selection (single selection) when adding cells at top?UITableView:在顶部添加单元格时保持选择(单选)?
【发布时间】:2016-05-30 16:20:34
【问题描述】:

此代码将在使用 reloadData 刷新 UITableView 后恢复单元格选择:

NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
[self.tableView reloadData];
[self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];

当然,当我在 TableView 顶部添加新单元格时,此解决方案不起作用。在顶部添加单元格时如何保持选择?

【问题讨论】:

    标签: ios objective-c uitableview selection reloaddata


    【解决方案1】:

    这实际上取决于您的数据源。简单情况下最常见的例子,如果是数组,那么每次选择一行就可以“记住”这个对象(tableView:didSelectRowAtIndexPath:

    myObjecyt = [myArray objectAtIndex:indexPath.row];
    

    ,在[table reloadData];之后,选择表索引:

    NSUInteger index = [myArray indexOfObject:myObject];
    if (index != NSNotFound) {
        NSINdexPath *selectedIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
        [self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    
    }
    

    但是,正如我所说,这实际上取决于很多因素,这只是基本设置的一个示例,其中表格填充了来自 myArray 的数据,没有重新排列和省略项目(并且只有一个部分)。

    【讨论】:

      【解决方案2】:

      试试这个:

      -(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
      {
          if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
              //end of loading
      
             int anyIndex = <any number of cell>
             NSIndexPath *anyIndexPath = [[NSIndexPath alloc] indexPathForRow:anyIndex inSection:0];
             UITableViewCell *anyCell = [table cellForRowAtIndexPath: firstIndexPath]
             anyCell.selectionStyle = UITableViewCellSelectionStyle.Default
      
          }
      }
      

      这应该可行。

      【讨论】:

      • 除了将第一个单元格的选择样式设置为UITableViewCellSelectionStyleDefault之外,还有什么作用吗?
      • 你想要什么?总是第一个被选中?
      • 如果您阅读了 OP,问题会询问如何在重新加载 UITableView 后保留对特定单元格的选择。不一定是第一个单元格...
      • 更新了我的答案请看一下。
      【解决方案3】:

      正如@Frane Poljak 所说,用户点击 tableView 表明该行中包含的对象是用户感兴趣的。您必须保留对象作为参考才能再次选择包含它的单元格。

      假设你有一个Object 的数组,你可以这样做

      enum Value
      {
          case NotSelected
          case Selected(object)
      }
      
      private var selectionState : Value = .NotSelected
      
      func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
          selectionState = Selected(dataSource[indexPath.row])
      }
      
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
          [...]
          switch selectionState {
          case .Selected(let object as! Object):
              if dataSource[indexPath.row] == object { cell.selected = true } 
              else { cell.selected = false }
          default : cell.selected = false
          }
      }
      

      【讨论】:

        【解决方案4】:

        您可以简单地跟踪您在顶部添加的单元格数量,并在此基础上使用selectRowAtIndexPath: 增加您选择的行的索引。

        或者,如果您有任何类型的 UI 元素(UIButton、UILabel 等),那么您可以在 didSelectRowAtIndexPath: 中设置该元素的标签,然后使用该标签访问具有该元素的单元格(重新加载后)类似UITableViewCell *selectCell = (UITableViewCell *)[[self.view viewWithTag:100] superview];

        【讨论】:

          猜你喜欢
          • 2015-01-19
          • 1970-01-01
          • 1970-01-01
          • 2011-12-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-31
          • 2011-01-01
          相关资源
          最近更新 更多