如何使用 xib 显示单元格?
在 iOS 13 之前的设备上,此方法会显示单元格,但自 iOS 13 起,它不会显示。
SurveyFacultyCell
是 .xib
自定义表格 View 单元格的名称
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:SurveyFacultyCell? = tblView.dequeueReusableCell(withIdentifier: "surveyFacultyCell") as? SurveyFacultyCell
if cell == nil {
tblView.register(UINib(nibName: "SurveyFacultyCell", bundle: nil), forCellReuseIdentifier: "surveyFacultyCell")
cell = tblView.dequeueReusableCell(withIdentifier: "surveyFacultyCell") as? SurveyFacultyCell
}
cell?.selectionStyle = .none
cell?.contentView.backgroundColor = UIColor.clear
let dictFaculty = arrFaculty[indexPath.row] as! NSDictionary
print(dictFaculty)
cell?.lblName.text = dictFaculty.string(forKey: "facultyName")
print(cell?.lblName.text as Any)
}
最佳答案
您注册 Xib 的方式错误。只需在 viewDidLoad()
处注册即可。
我告诉你如何Xib 注册
而不是你的逻辑
。
这是我的回答:
在 viewDidLoad() 上注册 xib
tableView.register(UINib(nibName: "SurveyFacultyCell", bundle: nil), forCellReuseIdentifier: "SurveyFacultyCell")
cellForRowAt:
let cell = tableView.dequeueReusableCell(withIdentifier: "surveyFacultyCell", for: indexPath) as! SurveyFacultyCell
... Your logic ...
return cell
完成
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(UINib(nibName: "SurveyFacultyCell", bundle: nil), forCellReuseIdentifier: "SurveyFacultyCell")
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "surveyFacultyCell", for: indexPath) as! SurveyFacultyCell
cell?.selectionStyle = .none
cell?.contentView.backgroundColor = UIColor.clear
let dictFaculty = arrFaculty[indexPath.row] as! NSDictionary
print(dictFaculty)
cell?.lblName.text = dictFaculty.string(forKey: "facultyName")
print(cell?.lblName.text as Any)
}
关于ios - 如何在 iOS 13.3 上使用 xib 显示 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59387219/