【问题标题】:How to display an UITableViewCell using xib on iOS 13.3?如何在 iOS 13.3 上使用 xib 显示 UITableViewCell?
【发布时间】:2019-12-18 07:23:00
【问题描述】:

如何使用 xib 显示单元格? 在 iOS 13 之前的设备上,此方法显示一个单元格,因为 iOS 13 它没有。 SurveyFacultyCell.xib 自定义表格视图单元格的名称

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

【问题讨论】:

  • tblView.register... 移动到viewDidLoad,在cellForRow 中不需要它
  • @Tj3n 这整个 tblView.register(UINib(nibName: "SurveyFacultyCell", bundle: nil), forCellReuseIdentifier: "surveyFacultyCell")
  • 我还建议从 Storyboard 或 SurveyFacultyCell (awakeFromNib()) 中设置 selectionStylecontentView.backgroundColor。将其设置为cellForRowAt 不是一个好主意
  • 在创建单元格的实例时,请使用as! SurveyFacultyCell 而不是as? SurveyFacultyCell,因为如果单元格类型不存在(这不应该发生,因为它应该就在那里),你的程序应该会崩溃
  • 您好像忘记归还手机了。在语句末尾添加返回单元格

标签: ios swift tableview xib


【解决方案1】:

您以错误的方式注册 Xib。只需在viewDidLoad() 注册即可。 我告诉你如何Xib registering 而不是你的logic

这是我的答案:

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

【讨论】:

  • cellForRow注册没有错,它可以正常工作。您改进了代码样式,但没有修复任何内容。
  • @Muju 我使用相同的编码实践,在 iphone8 或 iphone11 中没有问题
【解决方案2】:

把它放在你的 viewDidLoad 中

tblView.register(UINib(nibName: "SurveyFacultyCell", bundle: nil), forCellReuseIdentifier: "surveyFacultyCell") 

如果您要像这样注册一个单元格,dequeReusableCell 方法将为您创建一个单元格,如果没有单元格可以重用。 因此,您无需在 cellForRow 方法中检查 nil 单元格

【讨论】:

  • 在 iPhone 6s 中它工作正常,但在 iPhone 8 中它不工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-03
  • 1970-01-01
  • 1970-01-01
  • 2018-02-04
  • 2011-08-19
相关资源
最近更新 更多