【问题标题】:Get row number of UITableview with multiple sections获取具有多个部分的 UITableview 的行号
【发布时间】:2013-06-02 20:17:58
【问题描述】:

我有一个UITableView,里面有多个部分,每个部分都有多行。我想获取所选单元格相对于整个表格的行号,而不仅仅是部分。

示例:

  1. UITableView 中有两个部分,第 1 部分有 3 行,第 2 部分有 5 行。
  2. 当我选择第 2 节的第 2 行时,我应该在 didSelectRowAtIndexPath 方法中获取 5 作为行号,而不是获取 2 作为行号(与节相关)。

我尝试通过执行以下操作自己获取行号,但它似乎不起作用:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

NSLog(@"%d",indexPath.row);

int theRow =  indexPath.row;

NSLog(@"%d",theRow);
}

我正在考虑将行号存储在 intvariable 中,然后自己添加行号,但是尝试将 indexpath.row 存储在 theRow 中时代码崩溃。

请帮忙。谢谢

【问题讨论】:

  • 你拥有模型,所以你告诉表视图它有多少行。把所有东西都加起来。
  • @Wain 你能再解释一下吗?对不起,我没听懂你说的。
  • @SHA:您能解释一下为什么要使用“绝对”行号而不是“部分相对”行号吗?
  • @MartinR:从数组的那个(绝对行号)索引中获取一个值。
  • @SHA:好的,但您也可以考虑使用嵌套数组(将节号作为第一个索引,将行号作为第二个索引)作为数据源。

标签: ios objective-c uitableview didselectrowatindexpath


【解决方案1】:

以下是@Wain 解决方案的Swiftier 版本:

let rowNumber = Array(0..<indexPath.section).reduce(0) { $0 + tableView.numberOfRows(inSection: $1) } + indexPath.row

【讨论】:

    【解决方案2】:

    假设我在didSelectRowAt / didSelectItemAt 回调中实现了下面的代码......

    UITableView

    var index: Int = indexPath.row
    for i in 0..<indexPath.section {
        index += tableView.numberOfRows(inSection: i)
    }
    

    UICollectionView

    var index: Int = indexPath.item
    for i in 0..<indexPath.section {
        index += collectionView.numberOfItems(inSection: i)
    }
    

    示例:2 个部分,每个部分 3 行(项目)。第 1 节中选中的行(项目)1,index = 4

    【讨论】:

      【解决方案3】:

      我有一个大数据集,之前使用 for 循环的答案在较低部分对我造成了性能问题。最后我提前做了计算并加快了速度。

      private var sectionCounts = [Int:Int]()
      
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          let number = fetchedResultsController.sections?[section].numberOfObjects ?? 0
          if section == 0 {
              sectionCounts[section] = number
          } else {
              sectionCounts[section] = number + (sectionCounts[section-1] ?? 0)
          }
          return number
      }
      
      func totalRowIndex(forIndexPath indexPath: NSIndexPath) -> Int {
          if indexPath.section == 0 {
              return indexPath.row
          } else {
              return (sectionCounts[indexPath.section-1] ?? 0) + indexPath.row
          }
      }
      

      【讨论】:

        【解决方案4】:

        下面是这个概念在 Swift 中的更简洁的实现:

        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
            var rowNumber = indexPath.row
            for i in 0..<indexPath.section {
                rowNumber += self.tableView.numberOfRowsInSection(i)
            }
        
            // Do whatever here...
        }
        

        【讨论】:

        • 感谢您的回答。只是一个重命名编辑的问题: numberOfRowsInSection(i) 重命名为 numberOfRows(inSection: i)。
        【解决方案5】:

        这是 Wain 对上述答案的 Swift 改编

        class func returnPositionForThisIndexPath(indexPath:NSIndexPath, insideThisTable theTable:UITableView)->Int{
        
            var i = 0
            var rowCount = 0
        
            while i < indexPath.section {
        
                rowCount += theTable.numberOfRowsInSection(i)
        
                i++
            }
        
            rowCount += indexPath.row
        
            return rowCount
        }
        

        【讨论】:

          【解决方案6】:

          我不知道多个部分,但我可以给你一个部分...

          -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
          {
          NSInteger index=indexPath.row;
          NSString *string=[[NSString alloc]initWithFormat:@"%ld",(long)index];
          }
          

          从这里你可以得到行号,你可以把它保存到字符串中......

          【讨论】:

            【解决方案7】:
            NSInteger rowNumber = 0;
            
            for (NSInteger i = 0; i < indexPath.section; i++) {
                rowNumber += [self tableView:tableView numberOfRowsInSection:i];
            }
            
            rowNumber += indexPath.row;
            

            【讨论】:

            • ... 或 [self.tableView numberOfRowsInSection:i] - 表视图缓存从数据源返回的值。
            • 您正在计算行数。这是他们想要的吗?
            • @Wain,非常感谢。这就是我想要的。
            • @Wain 谢谢,你的帖子对我帮助很大。
            猜你喜欢
            • 2013-02-15
            • 1970-01-01
            • 1970-01-01
            • 2014-02-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多