【问题标题】:Get last cell in a UITableview section获取 UITableview 部分中的最后一个单元格
【发布时间】:2013-12-16 04:41:17
【问题描述】:

我在 UITableView 中有一个包含多行的部分。我希望获取该部分的最后一行或最后一个单元格以在其上添加披露指示符。

我想使用的一种方法是:

NSIndexPath *lastCellIndexPath = [NSIndexPath indexPathForItem:[self.tableView numberOfRowsInSection:2]-1 inSection:2];

有没有其他最好的方法来获取节上最后一个单元格的单元格或索引路径?

【问题讨论】:

  • 您可以从您在委托方法中设置的数据源中获取它。

标签: ios uitableview nsindexpath


【解决方案1】:
    NSInteger totalRow = [tableView numberOfRowsInSection:indexPath.section];//first get total rows in that section by current indexPath.
    if(indexPath.row == totalRow -1){
         //this is the last row in section.
    }

希望对你有帮助。

我在 tableView:willDisplayCell:forRowAtIndexPath: 方法中做到了这一点

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;

【讨论】:

    【解决方案2】:

    Swift 版本:

    let totalRows = tableView.numberOfRows(inSection: indexPath.section)
    //first get total rows in that section by current indexPath.
    if indexPath.row == totalRows - 1 {
        //this is the last row in section.
    }
    

    【讨论】:

      【解决方案3】:
          - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
      
              //Section 0
              if (indexPath.section == 0) {
      
                  // Is Last row?
                  if ([dataSouceArray count] == (indexPath.row+1)) {
                      //Yes
      
                      cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
      
      
                  }
                  else{
                      // other rows
      
                      cell.accessoryType = UITableViewCellAccessoryNone;
      
                  }
      
      
              }
      
      
          }
      

      【讨论】:

      • 你确定你没有在这行打错字吗:[NSIndexPath indexPathForRow:[NSIndexPath indexPathForRow:??
      • 你去 :) 但它是在 willDisplayCell 中而不是在 cellForRowAtIndexPath 中吗?
      【解决方案4】:

      您应该在 cellForRowAtIndexPath 方法中执行此操作。您可以轻松检测此单元格属于哪个部分以及它是否是最后一个。

      【讨论】:

        【解决方案5】:

        您可以从您在委托方法中设置的数据源中获取它, 您分配的数据源 numberOfRowsInSection 方法。

        [arrayStores lastObject];

        所以在cellForRowAtIndexPath方法中你可以很容易地检查它,

        【讨论】:

          【解决方案6】:

          我想知道我是怎么错过的。最简单的解决方案在这里.. 我根据我的要求写在 didSelectRowAtIndexPath 方法中。

          if (indexPath.row ==userAccountsList.count-1)
          {
              NSLog(@"last row it is ");
                 }
          

          在上面的代码中 userAccountsList 是我们传递给 tableview 的数组。

          【讨论】:

            【解决方案7】:

            Swift 4 版本:-

              let totalRow =
                        tableView.numberOfRows(inSection: indexPath.section)
                    if(indexPath.row == totalRow - 1)
                    {
                        return
                    }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-08-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多