迭代所有给定部分 id 的 UITableCells [英] Iterate over all the UITableCells given a section id

查看:25
本文介绍了迭代所有给定部分 id 的 UITableCells的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Swift,如何在给定部分 id(例如:部分 2 中的所有单元格)的情况下遍历所有 UITableCells?

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?":

要遍历部分 section 的所有 UITableCell,必须使用两种方法:

To iterate over all the UITableCells of a section section one must use two methods:

  • 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 的 UITableCells的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆