简体   繁体   English

快速更改图像不起作用

[英]Swift changing image doesn't work

EDIT: After changing the image to a label and transform a string ">" 90 degrees it is still not working correctly. 编辑:将图像更改为标签并转换字符串“>” 90度后,它仍无法正常工作。 I print out the values of the degrees and it works as expected but the transform is not happening. 我打印出度数的值,它可以按预期工作,但转换没有发生。

I have a totally strange behaviour in my iOS App. 我的iOS应用程序中有一个完全奇怪的行为。 I have a table view with section headers. 我有一个带有节标题的表格视图。 In these section headers are typical dropdown images. 在这些部分中,标题是典型的下拉图像。 One arrow down and one arrow up image. 向下的一个箭头和向上的一个箭头图像。 When I tap on the header section the cells display or hide dynamically. 当我点击标题部分时,单元格将动态显示或隐藏。 Now I want to change the image also according to that. 现在,我也想根据此更改图像。

This is the code that runs when I tap the section header: 这是我点击节标题时运行的代码:

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)
    }
}

Toggle Icon looks like this: 切换图标如下所示:

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")
    }
}

When I run this and click on different headers everything works as expected. 当我运行此程序并单击不同的标题时,一切正常。 But as soon as I close one section without opening another this section is broken. 但是,一旦我关闭一个部分却不打开另一个部分,此部分就会坏掉。 The code still works but it does not change the image. 该代码仍然有效,但不会更改图像。 It prints out: 它输出:

'Selected: 1'
true
Works

And when I use a breakpoint the code is called where it sets the icon to arrow-up. 当我使用断点时,该代码被称为将代码设置为向上箭头的代码。 However, it does not change the image. 但是,它不会更改图像。

I really don't understand what is happening. 我真的不明白发生了什么。 Is it not allowed to change an image more than once? 不允许多次更改图像吗?

Thanks for your help! 谢谢你的帮助!

I'm not sure if this is what cause the issue, but keep in mind that ALL UI Changes has be on the main thread. 我不确定这是否是导致问题的原因,但请记住,所有UI更改都位于主线程上。

on Swift 3, 在Swift 3上

try wrapping your call to update the image with something like 尝试用类似的方法包装您的电话以更新图像

DispatchQueue.main.async { }

Good luck :) 祝好运 :)

Ok I was able to solve it with my own implementation. 好的,我能够用自己的实现解决它。

I changed the else statement in selectedSection(sender: UIButton) to: 我将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()

So animating the whole section did not work. 因此,为整个部分设置动画效果不起作用。 Thanks @Mitesh jadav for his option but this does not close the other open sections so I sticked with my solution. 感谢@Mitesh jadav的选择,但这不会关闭其他打开的部分,因此我坚持使用我的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM