簡體   English   中英

在默認的UITableViewCell中顯示一個imageView

[英]Display an imageView in the default UITableViewCell

創建新的UITableView ,可以設置cell.imageView。 從理論上講,這不是應該顯示圖像嗎? 是在UITableViewCell內部實際顯示圖像以創建自定義單元格子類的唯一方法嗎?

這是我正在使用的代碼:

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = UITableViewCell (style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        cell.textLabel?.text = practices[indexPath.row].name
        cell.detailTextLabel?.text = practices[indexPath.row].address?.displayString()

//this doesn't show an image   
        cell.imageView?.clipsToBounds = true
        cell.imageView?.contentMode = .scaleAspectFill
        cell.imageView?.image = practices[indexPath.row].logo

        return (cell)
    }

您應該使一個單元出隊,而不是每次都分配一個新單元:

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    // Configure the cell
    cell.imageView?.image = practices[indexPath.row].logo
    return cell
}

正如其他人所建議的那樣,將測試圖像添加到您的xcassets中,以驗證問題是否出在Practices數組中。

在單元格中實際顯示圖像的唯一方法是創建服裝單元格嗎?

不,那不是真的。 您也可以按照以下步驟進行設置:

cell.imageView?.image = some UIImage

在你的代碼中

cell.imageView?.image = practices[indexPath.row].logo

請檢查practices[indexPath.row].logo實際是否具有UIImage

還要注意,請使用dequeueReusableCell

let cell = tableView.dequeueReusableCell(withIdentifier: "someCellName", for: indexPath)

而不是每次都在func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)中分配它

請檢查:

if practices[indexPath.row].logo is UIImage {
    print("My logo is not UIImage")
    cell.imageView?.image = nil
} else {
    print("My logo is UIImage")
    cell.imageView?.image = practices[indexPath.row].logo
}

暫無
暫無

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

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