[英]Swift changing image doesn't work
编辑:将图像更改为标签并转换字符串“>” 90度后,它仍无法正常工作。 我打印出度数的值,它可以按预期工作,但转换没有发生。
我的iOS应用程序中有一个完全奇怪的行为。 我有一个带有节标题的表格视图。 在这些部分中,标题是典型的下拉图像。 向下的一个箭头和向上的一个箭头图像。 当我点击标题部分时,单元格将动态显示或隐藏。 现在,我也想根据此更改图像。
这是我点击节标题时运行的代码:
func selectedSection(sender: UIButton) {
let previousSelected = self.selectedSection
self.selectedSection = sender.tag
if previousSelected != self.selectedSection {
// one section opens another gets closed
// workaround so the animation does not look awful for section headers
print("Previous: \(previousSelected)")
sectionHeaders[previousSelected].toggleIcon()
print("Selected: \(self.selectedSection)")
sectionHeaders[self.selectedSection].toggleIcon()
print("***********")
var indexPathToInsert = [IndexPath]()
var indexPathToDelete = [IndexPath]()
for i in 0..<self.menuItems[self.selectedSection].getSubItems().count {
indexPathToInsert.append(IndexPath(row: i, section: self.selectedSection))
}
for i in 0..<self.menuItems[previousSelected].getSubItems().count {
indexPathToDelete.append(IndexPath(row: i, section: previousSelected))
}
tableView.beginUpdates()
tableView.deleteRows(at: indexPathToDelete, with: .none)
tableView.insertRows(at: indexPathToInsert, with: .automatic)
tableView.endUpdates()
} else {
// close the section so the selected section is 0 again
self.selectedSection = 0
print("Previous: \(previousSelected)")
sectionHeaders[previousSelected].toggleIcon()
print("***********")
tableView.reloadSections([previousSelected], with: .none)
}
}
切换图标如下所示:
func toggleIcon() {
isOpen = !isOpen
print("\(isOpen)")
if isOpen {
print("Works")
self.sectionIcon.image = UIImage(named: "arrow-up")
} else {
print("Does not work")
self.sectionIcon.image = UIImage(named: "arrow-down")
}
}
当我运行此程序并单击不同的标题时,一切正常。 但是,一旦我关闭一个部分却不打开另一个部分,此部分就会坏掉。 该代码仍然有效,但不会更改图像。 它输出:
'Selected: 1'
true
Works
当我使用断点时,该代码被称为将代码设置为向上箭头的代码。 但是,它不会更改图像。
我真的不明白发生了什么。 不允许多次更改图像吗?
谢谢你的帮助!
我不确定这是否是导致问题的原因,但请记住,所有UI更改都位于主线程上。
在Swift 3上
尝试用类似的方法包装您的电话以更新图像
DispatchQueue.main.async { }
祝好运 :)
好的,我能够用自己的实现解决它。
我将selectedSection(sender:UIButton)中的else语句更改为:
// close the section so the selected section is 0 again
self.selectedSection = 0
print("Previous: \(previousSelected)")
sectionHeaders[previousSelected].toggleIcon()
print("***********")
var indexPathToDelete = [IndexPath]()
for i in 0..<self.menuItems[previousSelected].getSubItems().count {
indexPathToDelete.append(IndexPath(row: i, section: previousSelected))
}
tableView.beginUpdates()
tableView.deleteRows(at: indexPathToDelete, with: .left)
tableView.endUpdates()
因此,为整个部分设置动画效果不起作用。 感谢@Mitesh jadav的选择,但这不会关闭其他打开的部分,因此我坚持使用我的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.