简体   繁体   中英

Not getting all UITableViewCell from UITableView

I have a custom cell within my UITableView with a textField in it. I'm trying to read the textFields' values from table view.

To get the cells and values from textFields, I'm using the below code:

for var i = 0; i < myTableView.numberOfRowsInSection(0); i++ {  //5 records

    let index: NSIndexPath = NSIndexPath(forRow: i, inSection: 0)
    //getting the cell at position i 
    let cell = self.myTableView.cellForRowAtIndexPath(index) as! myCustomCell
    //Read data
    let newValue = cell.myTextField.text

}    

I'm not getting any compiler errors, but when I run the application, it sometimes fails at cell index 0, sometimes at cell index 3, it is at a random position. The error I'm getting is:

fatal error: unexpectedly found nil while unwrapping an Optional value

Which I understand because the cell is not present within myTableView.

Can anyone point me in the right direction to be able to read all the custom cells from my table view?

UITableViewCells are getting recycled. That's why its not safe to do it your way. You need to use a Data Model as others already pointed out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM