获取UITableview部分中的最后一个单元格
我在UITableView有一个部分有多个行。 我希望得到该部分的最后一行或最后一个单元格添加一个披露指标。
我想用的一种方法是:
NSIndexPath *lastCellIndexPath = [NSIndexPath indexPathForItem:[self.tableView numberOfRowsInSection:2]-1 inSection:2];
是否有任何其他最好的方式来获取单元格上的最后一个单元格的单元格或path?
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;
- (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; } } }
您应该在您的cellForRowAtIndexPath
方法中执行此操作。 你可以很容易地检测到这个单元属于哪个部分,以及它是否是最后一个。
您可以从您在numberOfRowsInSection
方法中设置的数据源(您在numberOfRowsInSection
方法中分配的数据源)中获取它。
[arrayStores lastObject];
所以在cellForRowAtIndexPath
方法中你可以很容易地检查它,
Swift版本:
var totalRow = tableView.numberOfRows(inSection: indexPath.section) //first get total rows in that section by current indexPath. if indexPath.row == totalRow - 1 { //this is the last row in section. }
我想知道我是如何错过的。 最简单的解决scheme是在这里..我写在didSelectRowAtIndexPath方法根据我的要求。
if (indexPath.row ==userAccountsList.count-1) { NSLog(@"last row it is "); }
在上面的代码中,userAccountsList是我们传递给tableview的数组。