Get last cell in a UITableview section

后端 未结 7 696
心在旅途
心在旅途 2021-01-17 10:17

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

相关标签:
7条回答
  • 2021-01-17 10:20

    Swift 4 version:-

      let totalRow =
                tableView.numberOfRows(inSection: indexPath.section)
            if(indexPath.row == totalRow - 1)
            {
                return
            }
    
    0 讨论(0)
  • 2021-01-17 10:29
        - (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;
    
                }
    
    
            }
    
    
        }
    
    0 讨论(0)
  • 2021-01-17 10:33

    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,

    0 讨论(0)
  • 2021-01-17 10:34

    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.

    0 讨论(0)
  • 2021-01-17 10:42

    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.
    }
    
    0 讨论(0)
  • 2021-01-17 10:44
        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;
    
    0 讨论(0)
提交回复
热议问题