2017-03-11 73 views
0

我有問題,使cellForRowAt工作,但不幸的是我的應用程序編譯後仍然空白。我使用的Xcode 8.3測試版3Swift cellForRowAt不顯示

import UIKit 

class myCell: UITableViewCell { 

    @IBOutlet var myLabel: UILabel! 
    @IBOutlet var myImageView: UIImageView! 
    override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

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

    // Configure the view for the selected state 
    } 
} 

我創建了一個食品類,使工作更輕鬆:

import Foundation 

class Food { 
var imageName = "" 
var description = "" 
var moreInfo = "" 

init(imageName: String, description: String, moreInfo: String) { 
    self.imageName = imageName 
    self.description = description 
    self.moreInfo = moreInfo 
    } 
} 

最後,這是TableViewController:

import UIKit 

class TableViewController: UITableViewController { 

    var foodArray: [Food] = [Food]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let food1 = Food(imageName: "cake.jpg", description: "Chocolate Cake", moreInfo: "There is nothing better than a great chocolate cake.") 
     let food2 = Food(imageName: "meringue.jpg", description: "Meringue & Berries", moreInfo: "I really don't like meringue but it's a nice photo.") 
     let food3 = Food(imageName: "peaches.jpg", description: "Grilled Peaches", moreInfo: "This would be perfect as a summer time dessert.") 
     let food4 = Food(imageName: "tiramisu.jpg", description: "Tiramisu", moreInfo: "One of my favorite Italian desserts. Yum.") 

     foodArray.append(food1) 
     foodArray.append(food2) 
     foodArray.append(food3) 
     foodArray.append(food4) 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 



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

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


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! myCell 
     let foodItem = foodArray[indexPath.row] 
     cell.myImageView.image = UIImage(named: foodItem.imageName) 
     cell.myLabel.text = foodItem.description 
     self.tableView.reloadData() 
     return cell 
    } 
} 

任何人都可以請讓我看看我做錯了什麼?

+0

該方法是否解僱?或者你說整個屏幕是空白的?因爲後者是一個不同的問題。 – dylanthelion

+0

應用程序是空的 – Virtuoso

+0

我會採取@reinier Melian的建議,除了完全刪除每個reloadData()調用。讓我知道這是否有效。 reloadData()調用你的表視圖的所有數據源方法,你可能在這裏有一個循環。另外,爲了將來的參考,UI更新應該在主線程上運行。 – dylanthelion

回答

1

你不應該叫self.tableView.reloadData()裏面你func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCellself.tableView.reloadData()

更換您的viewDidLoad方法這段代碼

override func viewDidLoad() { 
    super.viewDidLoad() 

    let food1 = Food(imageName: "cake.jpg", description: "Chocolate Cake", moreInfo: "There is nothing better than a great chocolate cake.") 
    let food2 = Food(imageName: "meringue.jpg", description: "Meringue & Berries", moreInfo: "I really don't like meringue but it's a nice photo.") 
    let food3 = Food(imageName: "peaches.jpg", description: "Grilled Peaches", moreInfo: "This would be perfect as a summer time dessert.") 
    let food4 = Food(imageName: "tiramisu.jpg", description: "Tiramisu", moreInfo: "One of my favorite Italian desserts. Yum.") 

    foodArray.append(food1) 
    foodArray.append(food2) 
    foodArray.append(food3) 
    foodArray.append(food4) 
} 

,並使用此代碼爲您func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell代碼

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! myCell 
    let foodItem = foodArray[indexPath.row] 
    cell.myImageView.image = UIImage(named: foodItem.imageName) 
    cell.myLabel.text = foodItem.description 
    return cell 
    } 

我希望這幫助你

+1

沒有理由在viewDidLoad()中調用reloadData()(數據源方法在viewWillAppear之後調用,我相信)。但是,在cellForRowAtIndexPath中重新調用reloadData()方法。 – dylanthelion

+0

仍然沒有工作... – Virtuoso

+0

@Virtuoso是模塊名稱問題呢?仍然不應該在cellForRowAtIndexPath中調用reloadData() –

0

的問題是,你打電話reloadData()所以要解決這個問題,從您的代碼中刪除:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! myCell 
    let foodItem = foodArray[indexPath.row] 
    cell.myImageView.image = UIImage(named: foodItem.imageName) 
    cell.myLabel.text = foodItem.description 

    return cell 
} 
0

您需要註冊類和使用方法添加重用標識符:

的tableView。的registerClass(UITableViewCell.self,forCellReuseIdentifier: 「細胞」)

在viewDidLoad中

而且添加這個方法,你不需要調用self.tableView.reloadData()內cellForRowAtIndex路徑方法。

希望它有幫助