在给定节ID的情况下迭代所有UITableCell [英] Iterate over all the UITableCells given a section id
问题描述
使用Swift,我如何在给定节ID的情况下迭代所有 UITableCells
(例如:第2节中的所有单元格)?
Using Swift, how can I iterate over all the UITableCells
given a section id (eg: all cells in section 2)?
我只看到这个方法: tableView.cellForRowAtIndexPath
,它返回给定绝对索引的1个单元格,所以它没有帮助。
I only see this method: tableView.cellForRowAtIndexPath
, which returns 1 cell given the absolute index, so it doesn't help.
是否有一种优雅而简单的方法?
Is there an elegant and easy way?
注意:我想为所有单元格中的AccesoryType设置为None部分,以编程方式,说:点击按钮后,或发生事件后(发生的事情与问题无关)
Note: I want to set the AccesoryType to None for all of the cells in a section, programatically, say: after a button is clicked, or after something happends (what happends is not relevant for the question)
我有UITableView的引用和该部分的索引。
I have the reference for the UITableView and the index of the section.
推荐答案
回答我自己的问题:我怎么能给定一个节ID,迭代所有 UITableCells
?:
To answer my own question: "how can I iterate over all the UITableCells
given a section id?":
迭代部分的所有UITableCells 部分
必须使用两种方法:
To iterate over all the UITableCells of a section section
one must use two methods:
-
tableView.number OfRowsInSection(section)
-
tableView.cellForRowAtIndexPath(NSIndexPath(forRow:row,inSection:section))
tableView.numberOfRowsInSection(section)
tableView.cellForRowAtIndexPath(NSIndexPath(forRow: row, inSection: section))
所以迭代是这样的:
// Iterate over all the rows of a section
for (var row = 0; row < tableView.numberOfRowsInSection(section); row++) {
var cell:Cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: row, inSection: section))?
// do something with the cell here.
}
在我的问题结束时,我还写了一封说明:注意:我想为节中的所有单元格设置AccesoryType为None,以编程方式。请注意,这是一个注释,而不是问题。
At the end of my question, I also wrote a note: "Note: I want to set the AccesoryType to None for all of the cells in a section, programatically". Notice that this is a note, not the question.
我最终这样做:
// Uncheck everything in section 'section'
for (var row = 0; row < tableView.numberOfRowsInSection(section); row++) {
tableView.cellForRowAtIndexPath(NSIndexPath(forRow: row, inSection: section))?.accessoryType = UITableViewCellAccessoryType.None
}
如果有更优雅的解决方案,请继续发布。
If there is a more elegant solution, go ahead and post it.
注意:我的表使用静态数据。
Note: My table uses static data.
这篇关于在给定节ID的情况下迭代所有UITableCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!