ios 在 Swift 中,如何遍历 UITableView 中的每个单元格,然后获取其属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31119988/
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
In Swift, how do I iterate through every cell in a UITableView and then get its properties?
提问by TIMEX
I have a generic UITableView and I want to go through every visible cell.
我有一个通用的 UITableView,我想浏览每个可见的单元格。
How can I do this in swift?
我怎样才能快速做到这一点?
回答by Rachel Harvey
I'm currently using this in one of my projects:
我目前在我的一个项目中使用它:
let cells = self.tableView.visibleCells as! Array<UITableViewCell>
for cell in cells {
// look at data
}
Here's another answer, but it's in objective-C: How can I loop through UITableView's cells?
这是另一个答案,但它在 Objective-C 中:如何遍历 UITableView 的单元格?
回答by Sven
You can't. UITableView doesn't keep any cells that are not visible. As soon as a cell moves completely off-screen it is removed and added to the reuse queue.
你不能。UITableView 不会保留任何不可见的单元格。一旦单元格完全移出屏幕,它就会被移除并添加到重用队列中。
回答by Russell Austin
Your table has a data source, often times the data source is an array of objects. The data source determines how many items are in the table. You could iterate over that data source and use cellForRowAtIndex path to get the cell object.
您的表有一个数据源,通常数据源是一个对象数组。数据源决定了表中有多少项。您可以遍历该数据源并使用 cellForRowAtIndex 路径来获取单元格对象。
The answer to this questionhas some information on cellForRowAtIndexPath
回答by sylvanaar
You said visible cells. You have the ability to iterate through those. Here is some sample code, you just need to get your own reference to the table.
你说的是可见细胞。你有能力遍历这些。这是一些示例代码,您只需要获得自己对表的引用。
let table = UITableView()
for cell in table.visibleCells() {
print(cell)
}
回答by Chris Deck
I created an array of cells and each time I create a cell in cellForRowAt I added it to the array, if it is not already in there. Code is below.
我创建了一个单元格数组,每次我在 cellForRowAt 中创建一个单元格时,我都会将它添加到数组中,如果它不在那里的话。代码如下。
var cells:[ProfileTableViewCell]! //initialize array at class level
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//your code to customize cell
if(!cells.contains(cell)){
self.cells.append(cell)
}
return cell
}
Then later you can loop through this array....
然后你可以循环遍历这个数组....
for cell in cells {
//do something with cell
}
回答by Brad Brighton
[NOTE: This answer was for the original question which explicitly included non-visible cells. Even after the mod though, unless the intent is for post-configuration presentation-specific cell properties, it's still appropriate to refer back to the datasource.]
[注意:此答案适用于明确包含不可见单元格的原始问题。即使在 mod 之后,除非意图是针对配置后呈现特定的单元格属性,否则引用回数据源仍然是合适的。]
Properly configured cells only have "properties" when they are visible, as they get reused as necessary.
正确配置的单元格只有在可见时才具有“属性”,因为它们会根据需要被重用。
Walk the elements of your datasource (the details of the iteration will vary depending on what type of structure you're using as a datasource) in order to determine the attributes you're looking for.
遍历数据源的元素(迭代的细节将根据您用作数据源的结构类型而有所不同)以确定您要查找的属性。
If you feel you have a valid reason to inspect the cells themselves anyway, you will need to keep a separate list of them from cellForRowAtIndexPath
time; whenever a new cell has been created, add it to the list.
如果您觉得自己有充分的理由检查细胞本身,那么您将需要保留一份单独的清单cellForRowAtIndexPath
;每当创建了新单元格时,将其添加到列表中。