xcode 如何检测 UITableView 中可见的单元格数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9094089/
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-09-14 23:10:41  来源:igfitidea点击:

How to detect how many cells are visible in a UITableView

objective-cxcodeuitableview

提问by user1179681

My table view covers part of the view. If I have 20 objects, I am displaying all of them in a tableview. But I want to know that how many cell are loaded that are visible to the user. (i.e. first 5 cells data is visible for me: when I scroll down, the remaining cells will load. Here I want to know without scrolling how many cell are loaded.)

我的表视图覆盖了视图的一部分。如果我有 20 个对象,我会在 tableview 中显示所有对象。但我想知道加载了多少用户可见的单元格。(即前 5 个单元格数据对我来说是可见的:当我向下滚动时,剩余的单元格将加载。在这里我想知道不滚动加载了多少个单元格。)

Is this possible?

这可能吗?

回答by Mihai Pan?iru

You can use [tableView visibleCells] count], which returns numbers of the table cells that are visible, if I understand correctly what you want.

[tableView visibleCells] count]如果我正确理解您想要什么,您可以使用,它返回可见的表格单元格的数量。

回答by Piyush Kashyap

The below code will give you the array of indexpath of cells currently visible

下面的代码将为您提供当前可见的单元格的索引路径数组

NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];

after getting the index path you can easily access the cell at a particular indexpath

获取索引路径后,您可以轻松访问特定索引路径中的单元格

回答by Ilanchezhian

Its usually : (tableView's height / cell row height ) + 1

它通常是: (tableView's height / cell row height ) + 1

回答by Antonio MG

Can you take the size of the visible part of the table, and then the size of one cell, and divide them to know how many of them fit in the screen?

你能把表格可见部分的大小,然后是一个单元格的大小,把它们分开来知道有多少个适合屏幕吗?