我面临的问题可能是对可重用细胞的概念缺乏了解.我要说,我要创建30行,每行都有一个UISwitch.
当我切换其中一个开关时,它的行为会影响另一个开关.重点是:据我所知,iOS不会立即创建所有这些,而是在TableView向上滚动时等待重用单元格和下来.
如何保留这些重用对象的副本并告诉iOS为交换机设置正确的值?
我已经考虑过将单元格附加到[UISwitch]但我无法将所有30个单元格放在那里,看看:
...
var switches = [UISwitch]()
...
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Field10Cell", for: indexPath) as! Field10TableViewCell
...
//cell.value is a UISwitch
if !switches.contains(cell.value) {
switches.append(cell.value)
}
return cell
}
Run Code Online (Sandbox Code Playgroud)
您可以创建一个集合,用于存储已按下开关的单元格的索引.
var activeSwitches = Set<IndexPath>()
Run Code Online (Sandbox Code Playgroud)
每当用户按下单元格上的开关时,您将其存储在集合上,如下所示:
activeSwitches.insert(indexPath)
Run Code Online (Sandbox Code Playgroud)
如果您需要检查交换机是否已激活,只需检查其容器单元的indexPath是否处于活动开关中,如下所示:
if activeSwitches.contains(indexPath) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
为了知道用户何时按下特定开关,我建议使用以下方法:
在Field10TableViewCell上创建协议并添加委托.
protocol Field10Delegate {
func didChangeSwitch(value: Bool, indexPath: IndexPath)
}
class Field10TableViewCell {
var delegate: Field10Delegate?
var indexPath: IndexPath?
@IBOutlet weak var fieldSwitch: UISwitch! // Can't use 'switch' as a variable name
@IBAction func switchValueChanged(_ sender: UISwitch) {
if let indexPath = indexPath {
delegate?.didChangeSwitch(value: sender.isOn, indexPath: indexPath)
}
}
Run Code Online (Sandbox Code Playgroud)创建单元格时,将视图控制器设置为委托
let cell = tableView.dequeueReusableCell(withIdentifier: "Field10Cell", for: indexPath) as! Field10TableViewCell
cell.delegate = self
cell.indexPath = indexPath
Run Code Online (Sandbox Code Playgroud)使您的视图控制器符合协议:
extension ViewController: Field10Delegate {
/* Whenever a switch is pressed on any cell, this delegate will
be called. This is a good place also to trigger a update to
your UI if it has to respond to switch changes.
*/
func didChangeSwitch(value: Bool, indexPath: IndexPath) {
if value {
activeSwitches.insert(indexPath)
} else {
activeSwitches.remove(indexPath)
}
updateUI()
}
}
Run Code Online (Sandbox Code Playgroud)通过上述内容,您可以随时了解哪些开关处于活动状态,您可以使用此信息处理出列单元格.
| 归档时间: |
|
| 查看次数: |
339 次 |
| 最近记录: |