Get last cell in a UITableview section

不想你离开。 提交于 2019-12-01 15:05:49
johnMa
    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.
    }

hope it helps.

I did this in tableView:willDisplayCell:forRowAtIndexPath: method

- (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;

            }


        }


    }
Unit Testing

Swift version:

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.
}

You should do this inside your cellForRowAtIndexPath method. You can easily detect that which section this cell belongs to and whether it's the last one.

You can get it from your datasource from which you set in delegate methods, the datasource you assign in numberOfRowsInSection method.

[arrayStores lastObject];

so in cellForRowAtIndexPath method you can easily check it,

I wonder how i did miss it. The easiest solution is here.. I wrote in didSelectRowAtIndexPath method according to my requirement.

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

In the above code userAccountsList is array which we are passing to tableview.

Swift 4 version:-

  let totalRow =
            tableView.numberOfRows(inSection: indexPath.section)
        if(indexPath.row == totalRow - 1)
        {
            return
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!