我的 tableView 中有单元格,有 2 个显示文本的标签。
单元格在 UITableViewCell
类型的detailCell 类中进行管理,该类引用两个标签的IBOutlet。这使我能够管理我的原型(prototype)单元并显示核心数据条目。
我在每个标签顶部添加了 UITextField
(边框样式不可见),我想在用户点击文本字段并开始编辑时隐藏标签。
问题是我无法弄清楚如何在不再次引用 viewController 中的标签的情况下从 viewController 管理标签(在原型(prototype)类中引用)。当然,这会引发错误:“Outlet 无法连接到重复内容”。
另一种方法是根据我在 viewController 中使用的 var isEditing: Bool ,通过从单元类管理标签来隐藏/显示标签,但我没有设法使用它在细胞类中。
有什么想法吗?
最佳答案
要实现这一目标,您必须执行如下操作:
-> 首先,您必须将所有文本字段委托(delegate)设置为 self
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let aCell : myCustomCell = tableView.dequeueReusableCell(withIdentifier: "aCell") as! myCustomCell
aCell.txtView1.delegate = self
return aCell
}
-> 其次,当用户单击文本字段时,您必须获取特定的单元格
func textFieldDidBeginEditing(_ textField: UITextField) {
//Get parent cell by traveling to the superview as your cell (Depends on your view hierarchy in your cell)
let aCell : myCustomCell = textField.superview?.superview as! myCustomCell
//Hide below label
aCell.lblData.hidden = true
}
--> 第三,当调用文本字段结束编辑时,您必须隐藏文本字段并再次显示标签
func textFieldDidEndEditing(_ textField: UITextField) {
let aCell : myCustomCell = textField.superview?.superview as! myCustomCell
//Hide below label
aCell.lblData.text = textField.text
aCell.lblData.hidden = false
}
关于ios - Swift 3. 尝试从 viewController 更新原型(prototype)单元类中声明的一些 IBOutlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42993446/