自定义tableviewcell中的textfield当滚动和访问时,然后崩溃



当我添加具有textfield和scroll tableview的自定义表图单元格时单元格从屏幕上消失,然后我去获得对crasing应用输入的textfield的访问值。

这是我去访问textfield并崩溃时的代码,

        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

滚动表滚动时,将重复使用单元格,因此,在尝试访问不在屏幕上的单元格时,您会得到零。请参考此链接。

您应该在访问该单元之前安全地解开单元格。

let index2 : IndexPath = IndexPath(row: 2, section: 0)
guard let tblCell2 = tblInfo.cellForRow(at: index2) as? MyInfromationCell else { return }

如果给定的IndexPath获得零,这将返回该功能。

相关内容

最新更新