Ios 取消选中Swift中的UITableview自定义复选框选项
我正在尝试使用UITableview创建一个自定义复选框列表,该列表允许单个选择(但是我应该能够选择我想要的任意多个选项)和一个自定义UITableCell 我的自定义单元格包含一个标签和一个ImageView,我只想更改IV on tap中包含的图像 我可以在didSelectRowAtIndexPath中将图像从未选中更改为已选中,但将其从选中更改为未选中时遇到问题Ios 取消选中Swift中的UITableview自定义复选框选项,ios,swift,uitableview,checkbox,Ios,Swift,Uitableview,Checkbox,我正在尝试使用UITableview创建一个自定义复选框列表,该列表允许单个选择(但是我应该能够选择我想要的任意多个选项)和一个自定义UITableCell 我的自定义单元格包含一个标签和一个ImageView,我只想更改IV on tap中包含的图像 我可以在didSelectRowAtIndexPath中将图像从未选中更改为已选中,但将其从选中更改为未选中时遇到问题 func tableView(tableView: UITableView, 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块被完全忽略
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")
}
此外,当我设法在单击时显示复选框图像时,我得到了如下结果
而如果我尝试通过添加以下内容来设置cellForRowAtIndexPath中所有单元格的复选框
cell.ivRiskCellImage.image = UIImage(named: "CheckedBox")
复选框勾号不再是半透明的,而是应该是白色的
有什么想法吗?我花了相当长的时间试图解决这个问题,但没有任何运气。而不是
让cell=tableView.dequeueReusableCellWithIdentifier…
我会使用
let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomCellVC
通过这种方式,您可以获得对要在其中更改图像的单元格的引用tableView.dequeueReusableCellWithIdentifier
用于委托方法tableView(tableView:UITableView,CellForRowatineIndexPath indexPath:NSIndexPath)
中,当您希望回收单元格以获得更好的性能时
还有
cell.ivRiskCellImage.image!。isEqual(UIImage(名为:“UncheckedBox”)
不起作用,因为UIImage(名为:“UncheckedBox”)
创建了一个新的UIImage,它与要检查它的UIImage不同。而不是让cell=tableView.dequeueReusableCellWithIdentifier…
我将使用
let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomCellVC
通过这种方式,您可以获得对要在其中更改图像的单元格的引用tableView.dequeueReusableCellWithIdentifier
用于委托方法tableView(tableView:UITableView,CellForRowatineIndexPath indexPath:NSIndexPath)
中,当您希望回收单元格以获得更好的性能时
还有
cell.ivRiskCellImage.image!。isEqual(UIImage(名为:“UncheckedBox”)
不起作用,因为UIImage(名为:“UncheckedBox”)
创建了一个新的UIImage,它与您要检查它的UIImage不同。作为fat,我知道您希望更改图像形式的检查和取消检查,反之亦然
表视图委托主要有两种方法
– tableView:didDeselectRowAtIndexPath:
– tableView:didSelectRowAtIndexPath:
参考链接::
尽管您可以在中执行相同的操作–tableView:DidDeceloseRowAtIndexPath:
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
{
print("tableView -> didSelectRowAtIndexPath")
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomCellVC
cell.backgroundColor = UIColor.clearColor()
cell.ivRiskCellImage.image = UIImage(named: "**unCheckedBox**")
}
作为fat,我理解您希望更改图像形式的选中和取消选中,反之亦然 表视图委托主要有两种方法
– tableView:didDeselectRowAtIndexPath:
– tableView:didSelectRowAtIndexPath:
参考链接::
尽管您可以在中执行相同的操作–tableView:DidDeceloseRowAtIndexPath:
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
{
print("tableView -> didSelectRowAtIndexPath")
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomCellVC
cell.backgroundColor = UIColor.clearColor()
cell.ivRiskCellImage.image = UIImage(named: "**unCheckedBox**")
}
谢谢最后我得到了以下结果:func tableView(tableView:UITableView,didselectRowatineXpath indexath:nsindexath){let cell=tableView.cellforRowatineXpath(indexath)as!RiskCustomCellVC cell.ivRiskCellImage.image=UIImage(名为:“CheckedBox”)}func tableView(tableView:UITableView,DidDecellRowatineXpath indexPath:NSIndexPath){let cell=tableView.cellForRowatineXpath(indexPath)as!RiskCustomCellVC cell.ivRiskCellImage.image=UIImage(名为:“UncheckedBox”)}非常感谢!我在不知不觉中犯了同样的错误,这其实很有道理!)谢谢最后我得到了以下结果:func tableView(tableView:UITableView,didselectRowatineXpath indexath:nsindexath){let cell=tableView.cellforRowatineXpath(indexath)as!RiskCustomCellVC cell.ivRiskCellImage.image=UIImage(名为:“CheckedBox”)}func tableView(tableView:UITableView,DidDecellRowatineXpath indexPath:NSIndexPath){let cell=tableView.cellForRowatineXpath(indexPath)as!RiskCustomCellVC cell.ivRiskCellImage.image=UIImage(名为:“UncheckedBox”)}非常感谢!我在不知不觉中犯了同样的错误,这其实很有道理!)谢谢我已经尝试过使用DidRowatineXpath,但没有任何运气。但是在像Markus指出的那样定义了我的单元格并允许多选之后,我似乎能够在didSelect中选择它,并在didSelect中取消选择它。谢谢。我已经尝试过使用DidRowatineXpath,但没有任何运气。但是在像马库斯指出的那样定义了我的单元格并允许多重选择之后,我似乎能够在didSelect中选择它,并在didSelect中取消选择它。