单元格在UITableView中显示为空白

时间:2017-07-06 15:22:35

标签: ios swift uitableview

我正在制作一个带tableView的视图控制器来展示个人新闻卡。我想一劳永逸地加载单元格(不像往常那样重复使用)。所以我这样做:

    tableView.register(UINib(nibName: "RatingNewsCell", bundle: nil), forCellReuseIdentifier: "RatingNewsCell")
    tableView.register(UINib(nibName: "PromoNewsCell", bundle: nil), forCellReuseIdentifier: "PromoNewsCell")

    var cells = [UITableViewCell]()

    let rating = RatingNewsCell()
    rating.delegate = self
    cells.append(rating)

    etc...

    self.items = cells

单元格加载如下:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    return items[indexPath.section]
}

单元格代码如下所示:

class RatingNewsCell: UITableViewCell
{
    var delegate: RatingNewsCellDelegate?

    @IBOutlet weak var shopNameLabel: UILabel!
    @IBOutlet weak var bouquetImageView: RoundImageView!
    @IBOutlet weak var floristImageView: RoundImageView!
    @IBOutlet weak var ratingControl: RatingPicker!

    override init(style: UITableViewCellStyle, reuseIdentifier: String?)
    {
        super.init(style: style, reuseIdentifier: "RatingNewsCell")
    }

    required init?(coder decoder: NSCoder)
    {
        super.init(coder: decoder)
    }

    override func awakeFromNib()
    {
        super.awakeFromNib()

        ratingControl.setSelected(newIndex: 0)
    }
}

在结果中我有一个表格视图,但是单元格显示为空白:

enter image description here

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

我建议您重构代码以使用tableView的数据源方法,尽管您只使用静态单元格。

enum Section: Int {
    case RatingNews
    case PromoNews
    static var count: Int { return Section.PromoNews.rawValue + 1 }
}

func numberOfSections(in tableView: UITableView) -> Int {
    return Section.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let section = Section(rawValue: indexPath.section)!
    switch section {
    case .PromoNews:
        let cell = tableView.dequeueReusableCell(withIdentifier: "RatingNewsCell", for: indexPath) as! RatingNewsCell
        return cell
    case .RatingNews:
        let cell = tableView.dequeueReusableCell(withIdentifier: "PromoNewsCell", for: indexPath) as! PromoNewsCell
        return cell
    }
}

答案 1 :(得分:-1)

您需要致电

tableView.delegate = self

tableView.dataSource = self