ios - UITableViewCell 自定义按钮图像未在 Swift 3 中更新

标签 ios swift uitableview swift3 xcode8

我的手机中有链接、共享和收藏夹三个按钮。我已在自定义单元类中手动设置图像。在 tableview 委托(delegate)的 willDisplay 方法中,我希望收藏夹图像在用户点击后更新。该服务在后端运行良好,并且正在从收藏夹中添加/删除对象,但图像没有更新。我尝试过设置背景图像而不是图像,并且还部署了为 Objective-C 规定的各种技术,但在我的情况下似乎没有任何效果。最初我将图像设置为与 DealsFullTableViewCell 中相同的图像

这是我正在谈论的 willdisplay 代码片段

if (self.dealArray[indexPath.row].imageUrl.characters.count > 0){
    let cell = tableView.dequeueReusableCell(withIdentifier: "DealsFullTableViewCell", for: indexPath) as! DealsFullTableViewCell

    if DataBaseReadServices().getFavourtiesforItemId(self.dealArray[indexPath.row].dealId) == nil {
        cell.favouritesButton.setImage(UIImage(named: "Favourites Blue Outline 20x20"), for: .selected)

    } else {
        cell.favouritesButton.setImage(UIImage(named: "Favourite Icon Blue 20x20"), for: .selected)
        print ("=============")
    }
}

我正在将 DealsFullTableViewCell 中的图像初始化为

let favouritesButtonImage = UIImage.fontAwesomeIcon(name: .starEmpty, textColor: Constants.AppColours().primaryColour, size: CGSize(width: 20, height: 20), backgroundColor: UIColor.clear)

self.favouritesButton.setImage(favouritesButtonImage, for: .normal)

最佳答案

请检查以下答案,如果不起作用,请更改线程。

            if(DataBaseReadServices().getFavourtiesforItemId(self.dealArray[indexPath.row].dealId) == nil){

                cell.favouritesButton.setImage(UIImage(named: "Favourites Blue Outline 20x20"), for: .selected)

            } else {

                 cell.favouritesButton.setImage(UIImage(named: "Favourite Icon Blue 20x20"), for: .normal)
              print ("=============")
            }`

如果上面的代码不起作用,请更改线程。

DispatchQueue.main.async {
//your code
}

谢谢

关于ios - UITableViewCell 自定义按钮图像未在 Swift 3 中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44156067/

相关文章:

ios - 从 UIView 层次结构创建 UIImage 时背景透明度丢失

ios - 如何在 MKMapView 的 MKAnnotation 上设置图像

ios - 如何使 UICollectionViewFlowLayout 留在 UICollectionView 中

ios - 如何通过单击 UITableViewCell 按钮将数据发送到另一个 ViewController

swift - 取消选择 TableView 中的所有开关

iphone - 从 cellForRowAtIndexPath 获取单元格编号

iphone - 在 XCode 4 中调用 segue 的正确方法是什么?

ios - 如何在swift中使用格式字符串将多个元素对齐到一个主要元素

Swift:CBCentralManager 找不到任何设备

ios - UIButton 上的操作不起作用