Ios show/hide 表格视图单元格内的视图
Ios show/hide views inside a tableview cell
我有一个带有自定义单元格的表格视图。下图显示了我的表格视图单元格的视图层次结构。
向表格视图添加行时,我使用以下代码隐藏了 'check Image' 图像视图。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "accountCell", for: indexPath) as! AccountsTableViewCell
cell.checkImage.isHidden = true
return cell
}
当单击一行时,我将再次显示图像视图。下面是代码
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "accountCell", for: indexPath) as! AccountsTableViewCell
cell.checkImage.isHidden = false
}
但问题是当我单击一行时没有任何反应。系统执行 cell.checkImage.isHidden = false
代码行,但 imageview 没有出现。它仍然是隐藏的。有人能告诉我我做错了什么吗?
您必须管理您的数组(数据源)!当您单击您的行时,更新该索引的数组并重新加载您的 table 视图。
示例:
你的数组应该有一些标志,比如 isChecked
将它的值从 true 切换到 false,反之亦然。
在您的 cellForRowAtIndexPath
中选中此标志并根据该标志显示或隐藏您的图像!
您无法在自己的单元格中跟踪单元格检查状态;单元格对象只是您数据的一个视图,当 table 视图滚动时,单元格将被重复使用。
我建议使用 Set<IndexPath>
。在您的 cellForRowAt
中,您可以检查集合的内容以确定是否应隐藏复选标记:
var checked = Set<IndexPath>()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "accountCell", for: indexPath) as! AccountsTableViewCell
cell.checkImage.isHidden = self.checked.contains(indexPath)
return cell
}
在您的 didSelectRowAt
中,您只需切换集合成员资格并重新加载行
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if self.checked.contains(indexPath) {
self.checked.remove(indexPath)
} else {
self.checked.insert(indexPath)
}
tableView.reloadRows(at:[indexPath], with:.fade)
}
首先要确定你的
tableview.delegate = self
索引中的 select 行的第二件事使用了该代码
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! AccountsTableViewCell
cell.checkImage.isHidden = false
}
我有一个带有自定义单元格的表格视图。下图显示了我的表格视图单元格的视图层次结构。
向表格视图添加行时,我使用以下代码隐藏了 'check Image' 图像视图。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "accountCell", for: indexPath) as! AccountsTableViewCell
cell.checkImage.isHidden = true
return cell
}
当单击一行时,我将再次显示图像视图。下面是代码
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "accountCell", for: indexPath) as! AccountsTableViewCell
cell.checkImage.isHidden = false
}
但问题是当我单击一行时没有任何反应。系统执行 cell.checkImage.isHidden = false
代码行,但 imageview 没有出现。它仍然是隐藏的。有人能告诉我我做错了什么吗?
您必须管理您的数组(数据源)!当您单击您的行时,更新该索引的数组并重新加载您的 table 视图。
示例:
你的数组应该有一些标志,比如 isChecked
将它的值从 true 切换到 false,反之亦然。
在您的 cellForRowAtIndexPath
中选中此标志并根据该标志显示或隐藏您的图像!
您无法在自己的单元格中跟踪单元格检查状态;单元格对象只是您数据的一个视图,当 table 视图滚动时,单元格将被重复使用。
我建议使用 Set<IndexPath>
。在您的 cellForRowAt
中,您可以检查集合的内容以确定是否应隐藏复选标记:
var checked = Set<IndexPath>()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "accountCell", for: indexPath) as! AccountsTableViewCell
cell.checkImage.isHidden = self.checked.contains(indexPath)
return cell
}
在您的 didSelectRowAt
中,您只需切换集合成员资格并重新加载行
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if self.checked.contains(indexPath) {
self.checked.remove(indexPath)
} else {
self.checked.insert(indexPath)
}
tableView.reloadRows(at:[indexPath], with:.fade)
}
首先要确定你的
tableview.delegate = self
索引中的 select 行的第二件事使用了该代码
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! AccountsTableViewCell
cell.checkImage.isHidden = false
}