Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last cell in a UITableview section

I have a section in UITableView which has multiple rows. I wish to get the last row or the last cell of the section to add a disclosure indicator on it.

One way I am thinking to use is:

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

Is there any other best way to get the cell or indexpath of the last cell on a section?

like image 914
tech_human Avatar asked Dec 16 '13 04:12

tech_human


3 Answers

    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;
like image 141
johnMa Avatar answered Oct 22 '22 22:10

johnMa


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.
}
like image 12
Unit Testing Avatar answered Oct 22 '22 22:10

Unit Testing


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

            }


        }


    }
like image 3
Vijay-Apple-Dev.blogspot.com Avatar answered Oct 22 '22 22:10

Vijay-Apple-Dev.blogspot.com