ios - 如何在 iOS 13.3 上使用 xib 显示 UITableViewCell?

标签 ios swift tableview xib

如何使用 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/

相关文章:

ios - UITextView.attributedText,如何设置numberOflines和lineBreakMode?

string - 获取 UITextView 中的当前段落?

xcode - 使用 tableView :moveRowAtIndexPath:toIndexPath: method 后将新订单保存到核心数据

ios - numberForRowsInSection 计数从到

ios - StackViews View 和阴影

ios - 在 iOS 中以编程方式调用带有访问代码的电话号码

ios - SwiftUI:具有计算属性的 ObservableObject

ios - Swift:第一个 TableView Cell 与 viewDidLoad 上的状态栏重叠

ios - 以编程方式检查是否设置了密码锁

ios - 如何更新可重用 tableView 单元格中 UITextfield 中的文本值,每个单元格中的值不同