当我添加具有textfield和scroll tableview的自定义tableview单元格时 单元格从屏幕上消失,然后我转到输入的文本字段的访问值,即删除应用程序。
以下是我访问文本字段并崩溃的代码,
let index0 : IndexPath = IndexPath(row: 0, section: 0)
let tblCell0 = tblInfo.cellForRow(at: index0)! as! MyInfromationCell
let strEmail = tblCell0.txtEmail?.text
let index1 : IndexPath = IndexPath(row: 1, section: 0)
let tblCell1 = tblInfo.cellForRow(at: index1)! as! MyInfromationCell
let strAboutMe = tblCell1.txtDescription?.text
let index2 : IndexPath = IndexPath(row: 2, section: 0)
let tblCell2 = tblInfo.cellForRow(at: index2)! as! MyInfromationCell
let strStudioName = tblCell2.txtStudioName?.text
let index3 : IndexPath = IndexPath(row: 3, section: 0)
let tblCell3 = tblInfo.cellForRow(at: index3)! as! MyInfromationCell
let strWebsite = tblCell3.txtWebsite?.tex
答案 0 :(得分:0)
当滚动表视图时,单元格被重用,因此在尝试访问不在屏幕上的单元格时会得到一个nil。请参阅此link。
在访问单元格之前,您应该安全地打开单元格。
let index2 : IndexPath = IndexPath(row: 2, section: 0)
guard let tblCell2 = tblInfo.cellForRow(at: index2) as? MyInfromationCell else { return }
如果给定的索引路径获得nil,则返回该函数。