2017-07-06 128 views
0

我正在製作一個視圖控制器tableView以呈現各個新聞卡片。我想一次性加載單元格(不像往常一樣重新使用它)。所以我這樣做:單元格在UITableView中空白

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

任何想法?

+1

爲什麼不使用適當的單元重用? – rmaddy

+0

你真的想要使用indexPath.section嗎? – kailoon

+0

@kailoon是的,我使用部分而不是單元格來使單元之間的偏移容易:D – Edward

回答

-1

你需要調用

tableView.delegate = self 

tableView.dataSource = self 
+0

這些行出現在代碼中。否則,tableView將是空的 – Edward

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 
    } 
}