gpt4 book ai didi

ios - 在 Swift 中取消选中 UITableview 自定义复选框选项

转载 作者:搜寻专家 更新时间:2023-11-01 06:19:41 24 4
gpt4 key购买 nike

我正在尝试使用允许单选的 UITableview 和自定义 UITableCell 制作自定义复选框列表(不过,我应该能够选择尽可能多的选项)。

我的自定义单元格包含一个 Label 和一个 ImageView,我只想在点击时更改 IV 中包含的图像。

我可以在我的 didSelectRowAtIndexPath 中将图像从未选中状态更改为已选中状态而不会出现问题,但我无法将其从已选中状态更改回未选中状态。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
print("tableView -> didSelectRowAtIndexPath")

let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomCellVC

cell.backgroundColor = UIColor.clearColor()
cell.ivRiskCellImage.image = UIImage(named: "CheckedBox")
}

我尝试通过将复选框图像添加为突出显示图像然后将其打开和关闭来玩弄 ImageView 的突出显示状态,但它只进入 if 但如果再次单击则忽略 if else

    if(cell.ivCellImage.highlighted == false)
{
print("Highligth is OFF")
cell.ivCellImage.highlighted = true
}
else if(cell.ivCellImage.highlighted == true)
{
print("Highligth is ON")
cell.ivCellImage.highlighted = false
}

以及检查 IV 中的图像,这个 if-else block 被完全忽略

    if(cell.ivRiskCellImage.image!.isEqual(UIImage(named: "UncheckedBox")))
{
print("Is unchecked!")
cell.ivRiskCellImage.image = UIImage(named: "CheckedBox")
}
else if(cell.ivRiskCellImage.image!.isEqual(UIImage(named: "CheckedBox")))
{
print("Is checked!")
cell.ivRiskCellImage.image = UIImage(named: "UnheckedBox")
}

此外,当我设法在点击时显示复选框图像时,我得到类似这样的东西

enter image description here

如果我尝试通过添加此设置为 cellForRowAtIndexPath 中的所有单元格设置复选框

cell.ivRiskCellImage.image = UIImage(named: "CheckedBox")

复选框不再是半透明的,而是白色的,它应该是这样的

enter image description here

有什么想法吗?我花了很多时间试图解决这个问题,但运气不佳。

最佳答案

我会使用 let cell = tableView.dequeueReusableCellWithIdentifier... 而不是

let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomCellVC

通过这种方式,您可以获得对要在其中更改图像的单元格的引用。tableView.dequeueReusableCellWithIdentifier 旨在用于委托(delegate)方法 tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) 当你想回收你的细胞以获得更好的性能时。

还有 cell.ivRiskCellImage.image!.isEqual(UIImage(named: "UncheckedBox")) 不起作用,因为 UIImage(named: "UncheckedBox")创建一个新的 UIImage,它与您要检查它的 UIImage 不同。

关于ios - 在 Swift 中取消选中 UITableview 自定义复选框选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36371903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com