swift - 根据Model更新UIButton的图像

标签 swift uitableview uibutton

我有一个 UITableView,其中每个单元格从我的模型类获取其数据,该模型类是我的 TableViewController 类中的一个数组。每个单元格的模型在cellForRowAtIndexPath中设置。我的模型有一个“isFavorited”属性。我的自定义单元格类有一个带有星星图像的 UIButton。

如果model.isFavorited == false,则UIButton图像是一个灰色的星星。

如果model.sFavorited == true,UIButton 图像应更改为黄色星形。

到目前为止,我已经尝试过这样处理:

private let normal = UIImage(named: "star")
private let selection = UIImage(named: "star highlighted")

class CustomCell: UITableViewCell {
      var model: Model? {
          didSet{
             favoriteButton.selected = (model?.isFavorite)!
          }
      }

      @IBOutlet var favoriteButton: UIButton!

      override func awakeFromNib() {
          super.awakeFromNib()
          favoriteButton.adjustsImageWhenHighlighted = true
          favoriteButton.setImage(normal, forState: .Normal)
          favoriteButton.setImage(selection, forState: .Highlighted)
          favoriteButton.setImage(selection, forState: .Selected)
      }

      @IBAction func favoriteTapped(sender: AnyObject) {
          //UPDATE THE MODEL
          model?.isFavorite = !(model?.isFavorite)!
      }

}

此代码成功地将星星从灰色更改为黄色,反之亦然,这要归功于调用 didSet 使用 (model?.isFavorite) 中的值更新 favoriteButton.selected bool 值!

但是,当我在表格 View 中滚动得足够远,并尝试返回到我喜欢的特定单元格时,该图像将重新显示为不喜欢的图像(灰色星形而不是黄色星形)。

如果当我点击按钮时我的模型正在更新,并且 UIButton 的图像由于此更改而在 didSet 期间进行了调整,那么为什么当滚动回此单元格时它会恢复到默认状态?

编辑: 我的代码在 cellForRowAtIndexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CustomCell
    cell.model = modelArray[indexPath.row]
    return cell
}

最佳答案

基本上,您可以使用基于点击设置的自定义图像对 UIButton 进行子类化。我遇到了同样的问题并使用以下方法解决了它:

class FavioriteButton: UIButton {
    //Images
    let normalImage = UIImage(named: "star")! as UIImage
    let selectedImage = UIImage(named: "star_highlighted")! as UIImage

    //Bool property
    var isSelected: Bool = false {
        didSet {
            if isSelected == true {
                self.setImage(normalImage, forState: .Normal)
            }else{
                self.setImage(selectedImage, forState: .Normal)
            }
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        self.addTarget(self, action: #selector(buttonClicked(_:)), forControlEvents: .TouchUpInside)
        self.isSelected = false
    }

    func buttonClicked(sender: UIButton) {
        if sender == self {
            if isSelected == true {
                isSelected = false
            }else{
                isSelected = true
            }
        }
   }
}

希望对您有帮助。

关于swift - 根据Model更新UIButton的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38256659/

相关文章:

ios - 为什么我的 TableView 更新这么慢?这是正常的吗?

ios - 在方向改变时改变整个用户界面

ios - 在 Core Data 中存储自定义对象的 NSMutableArray

ios - 将图像添加到 UINavigationBar 中的 UIButton

ios - 如何在 UIButton 的左侧和右侧放置文本

ios - Google Places API - 下一页 token 返回相同的结果

ios - 无法使用类型为 'addMutableTrackWithMediaType' 的参数列表调用 '(mediaType: String, preferredTrackID: Int)'

arrays - Swift - 将 url 内容排序到数组中

ios - UIWindow 添加 subview 位置

ios - 2 个根据内部文本调整大小并同时具有相等宽度的 UIButton?