OGeek|极客世界-中国程序员成长平台

标题: ios - 快速更改图像不起作用 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:18
标题: ios - 快速更改图像不起作用

编辑:将图像更改为标签并将字符串“>”转换 90 度后,它仍然无法正常工作。我打印出度数的值,它按预期工作,但没有发生转换。

我的 iOS 应用程序有一个完全奇怪的行为。我有一个带有部分标题的表格 View 。这些部分的标题是典型的下拉图像。一个向下箭头和一个向上箭头图像。当我点击标题部分时,单元格会动态显示或隐藏。现在我也想根据那个来改变图像。

这是当我点击部分标题时运行的代码:

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("revious: \(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("revious: \(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

当我使用断点时,会调用代码,它将图标设置为向上箭头。但是,它不会改变图像。

我真的不明白发生了什么。是不是不能多次更换图片?

感谢您的帮助!



Best Answer-推荐答案


我不确定这是否是导致问题的原因,但请记住,所有 UI 更改都在主线程上。

在 Swift 3 上,

尝试使用类似

的方式包装您的调用以更新图像
DispatchQueue.main.async { }

祝你好运

关于ios - 快速更改图像不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41047975/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4