簡體   English   中英

單元格中的UIButton不變

[英]UIButton in cell doesn't change

我有一個UITableView和一些自定義單元格,到目前為止一切正常。 我制作了一個自定義UITableViewCell類和帶有UIButton的原型,當單元格加載到表中時,我想提供自定義圖像和文本。

代碼背景:

這在UITableView控制器的tableView(tableView: UITableView, cellForRowAtIndexPath: NSIndexPath) ,該控制器將某些數據存儲在'catch'ivar中,后者又將要放置在按鈕中的圖像以及文本值。 定制的UITableViewCellImageTitleTableViewCell ,它僅具有IBOutlet屬性,如下所示-兩個標簽和myImageButton UIButton。

let cell: ImageTitleTableViewCell = tableView.dequeueReusableCellWithIdentifier("ImageTitleCell") as! ImageTitleTableViewCell
//Happily this never triggers.
assert(cell.myImageButton.imageView != nil, "Bad image button.")
cell.myImageButton.imageView!.image = catch.image
if catch.image != nil {
    cell.myImageButton.titleLabel!.text = ""
    println(cell.myImageButton.titleLabel!.text)
    // Always logs the the default value of the button text which isn't "".
} else {
    cell.myImageButton.titleLabel!.text = "None."
}
//These two work fine though.
cell.speciesLabel.text = catch.species
cell.dateLabel.text = catch.date.description
return cell

它也永遠不會放入圖像中。 我們可以確信catch.image在測試時確實包含有效的UIImage。

不要直接為按鈕設置圖像和文本。 使用func set<X>(forState:)func <X>ForState:)方法。

cell.myImageButton.setImage(catch.image, forState:.Normal)

cell.myImageButton.setTitle("", forState:.Normal)

代碼變成

cell.myImageButton.setImage(catch.image, forState:.Normal)
if catch.image != nil {
    cell.myImageButton.setTitle("", forState:.Normal)
    println(cell.myImageButton.titleForState(.Normal))
    // Always logs the the default value of the button text which isn't "".
} else { 
    cell.myImageButton.setTitle("None.", forState:.Normal)
}

使用以下更新圖像:

cell.myImageButton.setImage(UIImage(named: "imgTest.png"), forState: UIControlState.Normal) 

或您的代碼:

cell.myImageButton.setImage(catch.image, forState: UIControlState.Normal) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM