动态更新 tableview 部分中的 ImageView
Dynamically update an ImageView in a tableview section
我有一个包含 2 个(或更多)部分的 table 视图。我已经在其中添加了一个 ImageView,并且需要根据数组开头和 selecting/deselecting 单元格中包含的值更改图像视图。我创建的视图如下,
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let viewHeader = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 40))
buttonCheck = UIButton(type: .custom)
buttonCheck!.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
buttonCheck!.tag = section
buttonCheck!.addTarget(self, action: #selector(tapSection(sender:)), for: .touchUpInside)
viewHeader.addSubview(buttonCheck!)
}
这会很好地添加 ImageView,当我最初加载 table 数据时,我需要以编程方式设置图像视图。要更改我所做的图像视图,
if tableViewData.contains(where: self.tags.contains) {
buttonCheck!.setImage(UIImage(named: "CheckmarkCircle"), for: .normal)
} else {
buttonCheck!.setImage(UIImage(named: "DeselectedCheckmarkCircle"), for: .normal)
}
我在 didSelectRowAt
和 didDeselectRowAt
方法中调用了这个。这里的问题是,当我 select 来自第一部分(部分 = 0)的单元格时,它会影响第二部分(部分 = 1)header 图像视图。在其他工作中,当我 select 来自第一部分的单元格时,第二部分的 header 图像正在改变。我该如何解决这个问题?
您可以采用多种方法。快速而肮脏的方法是只调用 tableView.reloadData()
,这将强制使用 DataSource 中的当前数据重新加载 TableView 中的每个元素。
如果您想采用更高效的方法,您可以选择仅通过遍历 headers 部分来重新加载它们。这在 this 问题中得到了很好的回答。祝你好运。
我认为问题在于您每次调用 viewForHeaderInSection
时都会覆盖 buttonCheck
,这意味着它将始终包含对您创建的最后一个按钮的引用。
如果你创建一个字典来保存图像视图(索引是部分)会更好,就像在控制器范围上这样:
var imageViews: [Int: UIButton] = [:]
然后把viewForHeaderInSection
改成这样:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let viewHeader = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 40))
let buttonCheck = UIButton(type: .custom)
buttonCheck!.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
buttonCheck!.tag = section
buttonCheck!.addTarget(self, action: #selector(tapSection(sender:)), for: .touchUpInside)
imageViews[section] = buttonCheck
viewHeader.addSubview(buttonCheck!)
}
然后在 didSelect
和 didDeselect
上更新 imageView:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
imageViews[indexPath.section]?.setImage(UIImage(named: "CheckmarkCircle"), for: .normal)
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
imageViews[indexPath.section]?.setImage(UIImage(named: "DeselectedCheckmarkCircle"), for: .normal)
}
请考虑性能方面的问题,这可能不是最佳解决方案。最好创建一个自定义视图扩展 UITableViewHeaderFooterView 并考虑视图的可重用性。
我有一个包含 2 个(或更多)部分的 table 视图。我已经在其中添加了一个 ImageView,并且需要根据数组开头和 selecting/deselecting 单元格中包含的值更改图像视图。我创建的视图如下,
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let viewHeader = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 40))
buttonCheck = UIButton(type: .custom)
buttonCheck!.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
buttonCheck!.tag = section
buttonCheck!.addTarget(self, action: #selector(tapSection(sender:)), for: .touchUpInside)
viewHeader.addSubview(buttonCheck!)
}
这会很好地添加 ImageView,当我最初加载 table 数据时,我需要以编程方式设置图像视图。要更改我所做的图像视图,
if tableViewData.contains(where: self.tags.contains) {
buttonCheck!.setImage(UIImage(named: "CheckmarkCircle"), for: .normal)
} else {
buttonCheck!.setImage(UIImage(named: "DeselectedCheckmarkCircle"), for: .normal)
}
我在 didSelectRowAt
和 didDeselectRowAt
方法中调用了这个。这里的问题是,当我 select 来自第一部分(部分 = 0)的单元格时,它会影响第二部分(部分 = 1)header 图像视图。在其他工作中,当我 select 来自第一部分的单元格时,第二部分的 header 图像正在改变。我该如何解决这个问题?
您可以采用多种方法。快速而肮脏的方法是只调用 tableView.reloadData()
,这将强制使用 DataSource 中的当前数据重新加载 TableView 中的每个元素。
如果您想采用更高效的方法,您可以选择仅通过遍历 headers 部分来重新加载它们。这在 this 问题中得到了很好的回答。祝你好运。
我认为问题在于您每次调用 viewForHeaderInSection
时都会覆盖 buttonCheck
,这意味着它将始终包含对您创建的最后一个按钮的引用。
如果你创建一个字典来保存图像视图(索引是部分)会更好,就像在控制器范围上这样:
var imageViews: [Int: UIButton] = [:]
然后把viewForHeaderInSection
改成这样:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let viewHeader = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 40))
let buttonCheck = UIButton(type: .custom)
buttonCheck!.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
buttonCheck!.tag = section
buttonCheck!.addTarget(self, action: #selector(tapSection(sender:)), for: .touchUpInside)
imageViews[section] = buttonCheck
viewHeader.addSubview(buttonCheck!)
}
然后在 didSelect
和 didDeselect
上更新 imageView:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
imageViews[indexPath.section]?.setImage(UIImage(named: "CheckmarkCircle"), for: .normal)
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
imageViews[indexPath.section]?.setImage(UIImage(named: "DeselectedCheckmarkCircle"), for: .normal)
}
请考虑性能方面的问题,这可能不是最佳解决方案。最好创建一个自定义视图扩展 UITableViewHeaderFooterView 并考虑视图的可重用性。