在UITableview部分中获取最后一个单元格

tec*_*man 15 uitableview nsindexpath ios

我在UITableView中有一个有多行的部分.我希望得到该部分的最后一行或最后一个单元格,以便在其上添加披露指标.

我想用的一种方法是:

NSIndexPath *lastCellIndexPath = [NSIndexPath indexPathForItem:[self.tableView numberOfRowsInSection:2]-1 inSection:2];
Run Code Online (Sandbox Code Playgroud)

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

joh*_*nMa 45

    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.
    }
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.

我在tableView中执行了此操作:willDisplayCell:forRowAtIndexPath:方法

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
Run Code Online (Sandbox Code Playgroud)


小智 10

迅捷版:

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.
}
Run Code Online (Sandbox Code Playgroud)