空白 Table 查看单元格

Blank Table View Cell

我正在尝试 post 在 table 视图单元格中显示图片,但它总是返回空白。我为此创建了 3 个文件。

首先是一个带有图片信息的数据文件:

import Foundation

class Data {
    class Entry {
        let filename : String
        let heading : String
        init(fname : String, heading : String) {
            self.heading = heading
            self.filename = fname
        }
    }

    let pic = [
        Entry(fname: "Picture1.jpg", heading: "Heading 1"),

    ]

}

然后我有我的 table 视图控制器

import UIKit

class TableViewController: UITableViewController {

        let data = Data()
        override func viewDidLoad() {
            super.viewDidLoad()
            self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        }

        override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return 1
        }

        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return data.pic.count
        }

        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell
            let entry = data.pic[indexPath.row]
            let image = UIImage(named: entry.filename)
            cell.pic.image = image

            return cell
        }

}

第三个是单元格本身

import UIKit

class TableViewCell: UITableViewCell {

@IBOutlet var selfie: UIImageView!


    override func awakeFromNib() {
        super.awakeFromNib()

    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

}

我不知道为什么单元格返回空白。我将图像文件存储在支持文件中。

谁能帮帮我?

您必须将图像分配给 UITableViewCell 的 imageView

更新尝试:

cell.selfie.image = image

而不是 cell.pic.image = 图片

更新了我的回答:

确保在调用 dequeueReusableCellWithIdentifier:forIndexPath: 方法之前注册了 UITableViewCell 子类。只需将以下行添加到您的 viewDidLoad 方法中:

self.tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "Cell")

2 可能的原因:

第一个是,您的 UITableViewCell 包含一个名为 selfie 的图像视图,但您正在设置 pic

所以,试试 cell.selfie.image = UIImage(named: entry.filename);

第二个是,你确定你得到的是图像格式 UIImage(named: entry.filename); 吗?

希望这对您有所帮助..:)