UITableView 在 Swift 中始终显示基本单元格而不是自定义单元格
UITableView always displays basic cell instead of custom cell in Swift
我花了几个小时试图解决这个问题,但我的简单应用程序仍然显示基本单元格类型,而不是我的原型单元格。我知道在加载视图后使用标识符和注册,但它仍然显示只有一个标签的基本单元格。
到目前为止,这是我的代码:
我的原型正在使用这个 UITableViewCell:
class CoinTableViewCell: UITableViewCell {
@IBOutlet weak var coinIcon: UIImageView!
@IBOutlet weak var coinTitleLabel: UILabel!
@IBOutlet weak var holdings: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
UITableViewController:
class CoinTableViewController: UITableViewController {
var coins = ["Coin1","Coin2","Coin3"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(CoinTableViewCell.self, forCellReuseIdentifier: "currency_cell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return coins.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:CoinTableViewCell! = self.tableView.dequeueReusableCell(withIdentifier: "currency_cell", for: indexPath) as! CoinTableViewCell
let coinName = coins[indexPath.row]
cell.coinTitleLabel?.text = coinName
return cell!
}
}
如果有人能帮我解决这个问题,我将不胜感激!
您是直接在 Storyboard 的 tableview 上创建自定义单元格,对吗?
如果是这种情况,那么您不需要在 viewDidLoad
中注册单元格,因为故事板会处理这个问题。你只需 deque 它就可以了。
如果您手动注册它,您只需覆盖情节提要所做的操作并最终得到一个常规单元格,因为该单元格是从代码中实例化的,而不是从情节提要中实例化的。
干杯
我花了几个小时试图解决这个问题,但我的简单应用程序仍然显示基本单元格类型,而不是我的原型单元格。我知道在加载视图后使用标识符和注册,但它仍然显示只有一个标签的基本单元格。
到目前为止,这是我的代码:
我的原型正在使用这个 UITableViewCell:
class CoinTableViewCell: UITableViewCell {
@IBOutlet weak var coinIcon: UIImageView!
@IBOutlet weak var coinTitleLabel: UILabel!
@IBOutlet weak var holdings: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
UITableViewController:
class CoinTableViewController: UITableViewController {
var coins = ["Coin1","Coin2","Coin3"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(CoinTableViewCell.self, forCellReuseIdentifier: "currency_cell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return coins.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:CoinTableViewCell! = self.tableView.dequeueReusableCell(withIdentifier: "currency_cell", for: indexPath) as! CoinTableViewCell
let coinName = coins[indexPath.row]
cell.coinTitleLabel?.text = coinName
return cell!
}
}
如果有人能帮我解决这个问题,我将不胜感激!
您是直接在 Storyboard 的 tableview 上创建自定义单元格,对吗?
如果是这种情况,那么您不需要在 viewDidLoad
中注册单元格,因为故事板会处理这个问题。你只需 deque 它就可以了。
如果您手动注册它,您只需覆盖情节提要所做的操作并最终得到一个常规单元格,因为该单元格是从代码中实例化的,而不是从情节提要中实例化的。
干杯