【问题标题】:Get current indexPath row with multiple sections?获取具有多个部分的当前 indexPath 行?
【发布时间】:2014-02-25 10:55:25
【问题描述】:

我有一个包含多个部分的 UITableView。我没有使用每个部分使用一个数组的典型方法,而是使用一个数组。无论如何,我无法获取当前的 indexPath.row,就好像 tableview 中只有一个部分一样。

所以我想做的事情如下。如果section == 0,我只使用indexPath.row,但是如果section > 0,我想把上一个section的所有行加起来,得到当前section的当前行,得到总行数AS如果表格视图只有一个部分。

这是我当前的代码,但它无法正常工作,也许你们会看到我做错了什么。请告诉我:

if (indexPath.section == 0) {
        currentRow = indexPath.row;
    } else {
        NSInteger sumSections = 0;
        for (int i = 0; i < indexPath.section; i++) {
            int rowsInSection = [activitiesTable numberOfRowsInSection:i] + 1;
            sumSections += rowsInSection;
        }
        NSInteger row = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section].row + 1;
        currentRow = sumSections + row;
    }

【问题讨论】:

    标签: ios arrays row nsindexpath uitableview


    【解决方案1】:

    首先你不需要第一个 if 因为它不会通过 for-next。然后你添加一个不需要的,然后你可以像这样添加 indexPath.row:

        NSInteger sumSections = 0;
        for (int i = 0; i < indexPath.section; i++) {
            int rowsInSection = [activitiesTable numberOfRowsInSection:i];
            sumSections += rowsInSection;
        }
        currentRow = sumSections + indexPath.row;
    

    【讨论】:

    • 我已经搜索了几个月
    【解决方案2】:

    这是 swift 4 的版本。

    var currentIndex = 0
    var sumSections = 0;
    
    for index in 0..<indexPath.section {
        let rowsInSection = self.tableView.numberOfRows(inSection: index)
        sumSections += rowsInSection
    }
    
    currentIndex = sumSections + indexPath.row;
    

    【讨论】:

      【解决方案3】:

      这是我目前在 Swift 4 中使用的适合我的方法

      var currentRow = 0
      for section in 0..<indexPath.section {
              let rows = self.tableView.numberOfRows(inSection: section)
              currentRow += rows
      }
      
      currentRow += indexPath.row
      

      【讨论】:

        猜你喜欢
        • 2013-06-02
        • 2021-08-22
        • 1970-01-01
        • 2023-04-08
        • 1970-01-01
        • 2020-07-27
        • 2012-03-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多