Swift 如何键入check并访问命名空间
我有两个自定义的Swift 如何键入check并访问命名空间,swift,uitableview,Swift,Uitableview,我有两个自定义的UITableViewCell,当用户点击done按钮时,我想循环查看当前在我的tableView上的单元格。我使用tableView.visibleCells方法在我的tableView中循环。这是我的密码: func doneButtonTapped() { // TODO: - Need to collect user's input.... guard let node = node else { return } va
UITableViewCell
,当用户点击done
按钮时,我想循环查看当前在我的tableView
上的单元格。我使用tableView.visibleCells
方法在我的tableView
中循环。这是我的密码:
func doneButtonTapped() {
// TODO: - Need to collect user's input....
guard let node = node else {
return
}
var dict: Dictionary<String, Double>
let calculator = Calculator(module: node.value) // make it a failable initializer, so when user taps done without populating the inputs, it prompts them to
for cells in tableView.visibleCells {
if cells is NumericInputTableViewCell {
print("numeric cell")
// TODO: - Append to dictionary
cells. //Error
} else if cells is BooleanInputTableViewCell {
print("boolean cell")
//TODO: - Append to dictionary
cells. //Error
}
}
}
func donebuttonapped(){
//TODO:-需要收集用户的输入。。。。
保护let节点=节点else{
返回
}
字典
让calculator=calculator(module:node.value)//使其成为一个可失败的初始值设定项,因此当用户点击done而不填充输入时,它会提示他们
对于tableView.visibleCells中的单元格{
如果单元格为NumericiInputableViewCell{
打印(“数字单元格”)
//TODO:-附加到字典
单元格//错误
}如果单元格为BooleanInputableViewCell,则为else{
打印(“布尔单元格”)
//TODO:-附加到字典
单元格//错误
}
}
}
在循环遍历单元格的过程中,我检查并分离逻辑代码,这取决于for循环是否在if-else语句中找到NumericInputTableViewCell
或booleanInputableviewcell
。当我试图访问单元格的名称空间时,出现了一个错误,xcode无法自动完成。我应该能够在if语句中使用单元格
来访问这些自定义单元格。然而,我不能。我做错了什么?谢谢
更新
我得到的错误是“未解析标识符'cell'的用户”
在我的自定义单元格中,我得到了一个名为
numericTitleLabel
的属性,当我试图访问该属性时,它给了我一个错误。如果单元格是
检查单元格是否是某个类,如果你想访问属性,你应该强制转换你的visibleCell
,它是UITableViewCell
tableView.visibleCells.forEach ({ cell in
if let numbericCell = cell as? NumericInputTableViewCell {
print("numeric cell")
// TODO: - Append to dictionary
numbericCell..
} else if let booleanCell = cell as? BooleanInputTableViewCell {
print("boolean cell")
//TODO: - Append to dictionary
booleanCell...
}
})
您查看
可视单元格
以查看所表示的项目类型,这似乎很奇怪
我建议使用
indexPathsForVisibleRows
并使用返回的索引路径查询您的数据源,以确定显示的内容。错误是什么?你不能只拥有单元格。
。请澄清。仅仅因为Xcode不能为您自动完成,并不意味着您的代码无效。谢谢,请参阅更新用导致您问题的实际代码更新您的问题。我只是注意到函数visibleCells
只为您提供实际可见的单元格。