gpt4 book ai didi

ios - Swift cellForRowAt 不显示

转载 作者:行者123 更新时间:2023-11-28 12:28:30 25 4
gpt4 key购买 nike

我在使 cellForRowAt 工作时遇到问题,但不幸的是我的应用程序在编译后仍然是空白的。我正在使用 Xcode 8.3 beta 3。

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

我创建了一个 Food 类来简化工作:

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

谁能告诉我我做错了什么?

最佳答案

您需要使用以下方法注册类并添加重用标识符:

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

viewDidLoad中添加该方法

此外,您不需要在 cellForRowAtIndexPath 方法中调用 self.tableView.reloadData()

希望对你有帮助

关于ios - Swift cellForRowAt 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42736703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com