菜鸟教程小白 发表于 2022-12-11 18:18:07

ios - 快速更改图像不起作用


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

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

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

<pre><code>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(&#34;Previous: \(previousSelected)&#34;)
      sectionHeaders.toggleIcon()
      print(&#34;Selected: \(self.selectedSection)&#34;)
      sectionHeaders.toggleIcon()
      print(&#34;***********&#34;)

      var indexPathToInsert = ()
      var indexPathToDelete = ()

      for i in 0..&lt;self.menuItems.getSubItems().count {
            indexPathToInsert.append(IndexPath(row: i, section: self.selectedSection))
      }

      for i in 0..&lt;self.menuItems.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(&#34;Previous: \(previousSelected)&#34;)
      sectionHeaders.toggleIcon()
      print(&#34;***********&#34;)
      tableView.reloadSections(, with: .none)
    }
}
</code></pre>

<p>切换图标如下所示:</p>

<pre><code>func toggleIcon() {
    isOpen = !isOpen
    print(&#34;\(isOpen)&#34;)

    if isOpen {
      print(&#34;Works&#34;)
      self.sectionIcon.image = UIImage(named: &#34;arrow-up&#34;)
    } else {
      print(&#34;Does not work&#34;)
      self.sectionIcon.image = UIImage(named: &#34;arrow-down&#34;)
    }
}
</code></pre>

<p>当我运行它并单击不同的标题时,一切都按预期工作。但是一旦我关闭一个部分而不打开另一个部分,这个部分就被打破了。该代码仍然有效,但不会更改图像。打印出来:</p>

<pre><code>&#39;Selected: 1&#39;
true
Works
</code></pre>

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

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

<p>感谢您的帮助!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我不确定这是否是导致问题的原因,但请记住,所有 UI 更改都在主线程上。</p>

<p>在 Swift 3 上,</p>

<p>尝试使用类似 </p> 的方式包装您的调用以更新图像

<pre><code>DispatchQueue.main.async { }
</code></pre>

<p>祝你好运:)</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 快速更改图像不起作用,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41047975/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41047975/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 快速更改图像不起作用