19

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?

tech_human
  • 6,592
  • 16
  • 65
  • 107

7 Answers7

49
    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;
Haresh Ghatala
  • 1,996
  • 17
  • 25
johnMa
  • 3,291
  • 24
  • 37
13

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.
}
Satyam
  • 15,493
  • 31
  • 131
  • 244
Unit Testing
  • 261
  • 3
  • 5
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;

            }


        }


    }
  • Are you sure you didn't make a typo in this line: `[NSIndexPath indexPathForRow:[NSIndexPath indexPathForRow:`?? – sha Dec 16 '13 at 04:59
  • There you go :) But is it in willDisplayCell and not in cellForRowAtIndexPath? – sha Dec 16 '13 at 06:08
1

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.

sha
  • 17,824
  • 5
  • 63
  • 98
1

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.

Narasimha Nallamsetty
  • 1,215
  • 14
  • 16
1

Swift 4 version:-

  let totalRow =
            tableView.numberOfRows(inSection: indexPath.section)
        if(indexPath.row == totalRow - 1)
        {
            return
        }
dinesh sharma
  • 577
  • 10
  • 20
0

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,

Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121