简体   繁体   English

如何获取NSIndexPath部分中的行数

[英]How to get number of rows in NSIndexPath section

I want to get the number of rows in an NSIndexPath section. 我想获取NSIndexPath部分中的行数。 For all my searching here & in Apple's docs I can't find how to get this value. 对于我在这里和Apple的文档中的所有搜索,我找不到如何获得这个值。 Any clues on it? 有什么线索吗?

My specific purpose is to use that to determine whether or not the selected row is the last row in the section so if anyone has a suggestion for determining that by another means that would be as good. 我的具体目的是使用它来确定所选行是否是该部分中的最后一行,因此如果有人建议通过另一种方法确定该行是一样好的话。

Everything is in docs 一切都在文档中

numberOfRowsInSection: numberOfRowsInSection:

int rows = [table numberOfRowsInSection:indexPath.section];

唯一的方法是询问表的数据源:

NSUInteger rowCount = [tableView.dataSource tableView:tableView numberOfRowsInSection:indexPath.section];

Here's how I do it in my case: 以下是我在我的案例中的表现:

The table section holds a list of objects stored in NSArray , hence the amount of rows in section is the amount of objects in the array. 表部分包含存储在NSArray中的对象列表,因此节中的行数是数组中对象的数量。 In your case - if row is not the object from array - just do the check 在你的情况下 - 如果row不是数组中的对象 - 只需进行检查

if ([indexPath row] + 1 == [_objectsArray count]) {
    ...

But if your items in table are not held in a collection, you can easily retrieve number of rows in section just by calling appropriate method on tableView object in your didSelectRow.. delegate method: 但是如果表中的项目没有保存在集合中,只需在didSelectRow.. tableView对象上调用适当的方法,就可以轻松地检索节中的行数didSelectRow..委托方法:

NSInteger numberOfRows = [tableView numberOfRowsInSection:[indexPath section]];

You can call tableView:numberOfRowsInSection: method of the UITableViewDataSource , passing the section number. 您可以调用tableView:numberOfRowsInSection: UITableViewDataSource方法,传递节号。 However, it is your own code that produces this number based on what's in the model, so it may make sense to look at the implementation of tableView:numberOfRowsInSection: in your data source to see if you could get the same answer through an alternative path. 但是,根据模型中的内容生成此数字是您自己的代码,因此在数据源中查看tableView:numberOfRowsInSection:的实现可能是有意义的,看看您是否可以通过备用路径获得相同的答案。

If you are talking about on a UITableView, you can ask it's dataSource, which is somewhat likely to be yourself :-) 如果你在谈论UITableView,你可以问它的dataSource,这有点可能是你自己:-)

[self.tableView.dataSource tableView: self.tableView numberOfRowsInSection: indexPath.section];

// or, if you are the dataSource...
[self tableView: self.tableView numberOfRowsInSection: indexPath.section];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM