2017-03-11 39 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路径方法。

希望它有帮助