gpt4 book ai didi

ios - 向下滚动点击按钮继续下一个屏幕后如何验证 CustomCell 中的 UITextField - Swift 2.3

转载 作者:行者123 更新时间:2023-11-30 12:38:47 24 4
gpt4 key购买 nike

我有一个动态表,在自定义单元格中包含多个UITextFields。但是,当我滚动到 TableView 底部并点击“继续”按钮时,我在获取 UITextFieldstext 时遇到问题,滚动后退出屏幕。换句话说,我收到错误

fatal error :在解包可选值时意外发现 nil。

当我向下滚动到屏幕底部并点击继续按钮。

表格 View Controller :

class RNSContactInfoTableViewController: UITableViewController, UITextFieldDelegate, UITextViewDelegate {
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

if (indexPath.row == 0) {
//static street address cell
let streetAddressCell = tableView.dequeueReusableCellWithIdentifier("streetAddressCell", forIndexPath: indexPath) as! StreetAddressCell
setStreetAddressTextFieldValue(streetAddressCell)
return streetAddressCell
} else if (indexPath.row == 1) {
//static apt or suite cell
let aptOrSuiteCell = tableView.dequeueReusableCellWithIdentifier("aptOrSuiteCell", forIndexPath: indexPath) as! AptOrSuiteCell
setAptOrSuiteTextFieldValue(aptOrSuiteCell)
return aptOrSuiteCell
} else if (indexPath.row == 2) {
//static city cell
let cityCell = tableView.dequeueReusableCellWithIdentifier("cityCell", forIndexPath: indexPath) as! CityCell
setCityTextFieldValue(cityCell)
return cityCell
} else if (indexPath.row == 3) {
//static state cell
let stateCell = tableView.dequeueReusableCellWithIdentifier("stateCell", forIndexPath: indexPath) as! StateCell
setStateTextFieldValue(stateCell)
return stateCell
} else if (indexPath.row == 4) {
//static zip code cell
let zipcodeCell = tableView.dequeueReusableCellWithIdentifier("zipcodeCell", forIndexPath: indexPath) as! ZipCodeCell
setZipcodeTextFieldValue(zipcodeCell)
return zipcodeCell
} else {
//default cell
let cell = UITableViewCell()
return cell
}
}

//--------------------------------------------------------
// MARK: Button Tapped Methods
//--------------------------------------------------------

@IBAction func reviewServiceOrderButtonTapped(sender: UIButton) {

//Dismiss keyboard when button is tapped
self.view.endEditing(true)

//Set user fields
//<==========THE LINE BELOW IS CRASHING============>
let streetAddressCell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 4, inSection: 0)) as! StreetAddressCell
//<================================================>
let streetAddressStr = streetAddressCell.streetAddressTextField.text

let aptOrSuiteCell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 5, inSection: 0)) as! AptOrSuiteCell
let aptOrSuiteStr = aptOrSuiteCell.aptOrSuiteTextField.text

let cityCell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 6, inSection: 0)) as! CityCell
let cityStr = cityCell.cityTextField.text

let stateCell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 7, inSection: 0)) as! StateCell
let stateStr = stateCell.stateTextField.text

let zipcodeCell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 8, inSection: 0)) as! ZipCodeCell
let zipcodeStr = zipcodeCell.zipcodeTextField.text

// Check for empty fields
if (streetAddressStr!.isEmpty || cityStr!.isEmpty || stateStr!.isEmpty || zipcodeStr!.isEmpty || firstNameStr!.isEmpty || lastNameStr!.isEmpty || emailStr!.isEmpty || phoneAreaCodeStr!.isEmpty || phonePrefixStr!.isEmpty || phoneLineNumberStr!.isEmpty)
{
// Display error message
self.displayAlertMessage(EMPTY_REQUIRED_FIELDS_ERROR_TITTLE, message: EMPTY_REQUIRED_FIELDS_ERROR_MESSAGE)
return;
}

}
}

最佳答案

正如您所发现的,UITableView 所做的事情之一就是释放当前未显示的 UITableViewCell。这部分意味着 cellForRowAtIndexPath 将为当前不可见的单元格返回 nil

您需要在单元格发生更改时以及在单元格滚出屏幕之前从单元格中提取数据(请参阅各种 UITextFieldDelegate 方法),而不是稍后尝试查找单元格本身。

与这个特定问题无关,但还值得注意的是,当您上下滚动时,它将重用相同类型的单元格。

关于ios - 向下滚动点击按钮继续下一个屏幕后如何验证 CustomCell 中的 UITextField - Swift 2.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42518511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com