ios 获取 UITableview 部分中的最后一个单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20603598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:47:19  来源:igfitidea点击:

Get last cell in a UITableview section

iosuitableviewnsindexpath

提问by tech_human

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.

我在 UITableView 中有一个包含多行的部分。我希望获得该部分的最后一行或最后一个单元格以在其上添加披露指示符。

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?

有没有其他最佳方法可以获取部分上最后一个单元格的单元格或索引路径?

回答by 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

我在tableView:willDisplayCell:forRowAtIndexPath:方法中做了这个

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;

回答by 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.
}

回答by Vijay-Apple-Dev.blogspot.com

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

            }


        }


    }

回答by sha

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

您应该在您的cellForRowAtIndexPath方法中执行此操作。您可以轻松检测此单元格属于哪个部分以及它是否是最后一个。

回答by dinesh sharma

Swift 4 version:-

斯威夫特 4 版本:-

  let totalRow =
            tableView.numberOfRows(inSection: indexPath.section)
        if(indexPath.row == totalRow - 1)
        {
            return
        }

回答by Narasimha Nallamsetty

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

我想知道我是怎么错过的。最简单的解决方案在这里..我根据我的要求在 didSelectRowAtIndexPath 方法中编写。

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

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

在上面的代码中 userAccountsList 是我们传递给 tableview 的数组。

回答by Toseef Khilji

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

您可以从您在委托方法中设置的数据源中获取它,您在方法中分配的数据源 numberOfRowsInSection

[arrayStores lastObject];

[arrayStores lastObject];

so in cellForRowAtIndexPathmethod you can easily check it,

所以在cellForRowAtIndexPath方法中你可以很容易地检查它,