本文介绍了从.xib创建的自定义UITableViewCell不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我迷路了.我进行了搜索,找不到我的自定义单元格未显示的原因.
I'm lost. I searched and searched and cannot find the reason why my custom cell isn't displayed.
// ProfileCell.swift:
import UIKit
import QuartzCore
class ProfileCell: UITableViewCell {
@IBOutlet weak var profileNameLabel: UILabel!
@IBOutlet weak var profilePictureView: UIImageView!
}
第二个默认TableViewCell正常显示.我在Interface Builder中没有缺少约束或任何错误.在ProfileCell.xib身份选项卡中选择ProfileCell作为自定义类.
The second default TableViewCell is displayed normally. I have no missing constraints in Interface Builder or any Errors. ProfileCell is selected in the ProfileCell.xib identity tab as the custom class.
// MoreTableViewControllerIB.swift
import UIKit
class MoreTableViewControllerIB: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// this cell is missing
if indexPath.row == 0 {
tableView.register(UINib(nibName: "ProfileCell", bundle: nil), forCellReuseIdentifier: "ProfileCell")
let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell", for: indexPath) as! ProfileCell
cell.profileCommentLabel.text = "Test Comment Label"
cell.profilePictureView.image = UIImage(named:"profile_picture_test")
return cell
// this cell is displayed perfectly
}else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "statisticsCell") ?? UITableViewCell(style: .default, reuseIdentifier: "statisticsCell")
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
cell.textLabel?.text = "Statistics"
cell.imageView.image = UIImage(named:"statistics")
return cell
// has to return a cell in every scenario
}else{
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
return cell
}
}
}
推荐答案
tableView.register(UINib(nibName: "ProfileCell", bundle: nil), forCellReuseIdentifier: "ProfileCell")
将此行添加到viewDidLoad或viewWillApppear
add this line in viewDidLoad or viewWillApppear
这篇关于从.xib创建的自定义UITableViewCell不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!