我的 UITableView 中有一个自定义单元格,其中有一个 textField。我正在尝试从 TableView 中读取文本字段的值。
要从 textFields 中获取单元格和值,我使用以下代码:
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
}
我没有收到任何编译器错误,但是当我运行应用程序时,它有时会在单元格索引 0 处失败,有时会在单元格索引 3 处失败,它位于随机位置。我得到的错误是:
fatal error: unexpectedly found nil while unwrapping an Optional value
这是我理解的,因为单元格不在 myTableView 中。
任何人都可以指出正确的方向,以便能够从我的表格 View 中读取所有自定义单元格吗?
最佳答案
UITableViewCells 正在回收。这就是为什么按你的方式做是不安全的。正如其他人已经指出的那样,您需要使用数据模型。
关于ios - 没有从 UITableView 获取所有 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35511606/