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
Get last cell in a UITableview section
提问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 cellForRowAtIndexPath
method. 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
numberOfRowsInSection
method.
您可以从您在委托方法中设置的数据源中获取它,您在方法中分配的数据源
numberOfRowsInSection
。
[arrayStores lastObject];
[arrayStores lastObject];
so in cellForRowAtIndexPath
method you can easily check it,
所以在cellForRowAtIndexPath
方法中你可以很容易地检查它,