在Tableview iOS中,当单元格位于屏幕之外(不可见)时,尝试访问所有单元格数据时获取nil



我的表视图中有6个自定义单元格。每个单元格的数据我都需要检查用户是否填写了提交按钮,如果所有单元格的信息都填写了,我需要将这些信息传递给服务器,因为在发送服务器之前,所有单元格都必须填写。如果用户遗漏了要填充的内容,我必须抛出警报来填充该单元格。

但是,如果手机在屏幕之外,我得到的手机是零。

@IBAction func submitBtnAction(_ sender: Any) {

let dictionary = self.fetchSelectedCellsInfo()
//call api here by passing dictionary
}
func fetchSelectedCellsInfo() -> [[String: String]] {

// self.appSurveyTableview.reloadData()
if eachIndexInfo.count > 0 {
for (index, item) in eachIndexInfo.enumerated() {

let cellId = activeCells[index]
let indexPath = IndexPath.init(row:index, section: 0)
switch cellId.identifier {

case .yesNoCell:

let yesNoCell = self.appSurveyTableview.cellForRow(at: indexPath) as? YesNoTableViewCell
if let savedFlag = yesNoCell?.savedFlagValue, savedFlag.isEmpty == false {
selectedArrDictionary.append(["yesNoCell.savedFlagValue":savedFlag])
} else {
self.showBasicAlert(title: "Please provide feedback for (item.question ?? "") ", message: "")
}

break

case .TextWithCheckBoxesCell:
let textWithCheckBoxCell = self.appSurveyTableview.cellForRow(at: indexPath) as? TextWithCheckBoxesTableViewCell
if let selectedOption = textWithCheckBoxCell?.selectedOption, selectedOption.isEmpty == false {
selectedArrDictionary.append(["savedFlagValue":selectedOption])
} else {
self.showBasicAlert(title: "Please provide feedback for (item.question ?? "") ", message: "")
}
break

case .textWithRadioBtnsCell:
let textWithRadioBtnsCell = self.myTableview.cellForRow(at: indexPath) as? TextWithRadioButtonsTableViewCell
if let selectedOption = textWithRadioBtnsCell?.selectedOption, selectedOption.isEmpty == false {
selectedArrDictionary.append(["savedFlagValue":selectedOption])
} else {
self.showBasicAlert(title: "Please provide feedback for (item.question ?? "") ", message: "")
}
break

case .ratingCell:
let ratingCell = self.myTableview.cellForRow(at: indexPath) as? RatingTableViewCell
if let labelText = ratingCell?.ratingTextLabel.text, labelText.isEmpty == false {
selectedArrDictionary.append(["savedFlagValue":labelText])
} else {
self.showBasicAlert(title: "Please provide feedback for (item.question ?? "") ", message: "")
}
break

case .ratingWithTextBoxCell:
let ratingWithTextBoxCell = self.myTableview.cellForRow(at: indexPath) as? RatingWithTextBoxTableViewCell
if let ratingTextLabelText =  ratingWithTextBoxCell?.ratingTextLabel.text, let textViewText = ratingWithTextBoxCell?.textView.text, ratingTextLabelText.isEmpty == false && textViewText.isEmpty == false{
selectedArrDictionary.append(["savedFlagValue":ratingTextLabelText])
} else {
self.showBasicAlert(title: "Please provide feedback for (item.question ?? "") ", message: "")
}
break

case .appTextBoxCell:
let textWithTextBoxCell = self.myTableview.cellForRow(at: indexPath) as? TextboxTableCell
if let textViewText = textWithTextBoxCell?.textView.text, textViewText.isEmpty == false {
selectedArrDictionary.append(["savedFlagValue":textViewText])
} else {
self.showBasicAlert(title: "Please provide feedback for (item.question ?? "") ", message: "")
}
break

case .none:
break
}
}
}
return selectedArrDictionary
}

如何访问所有单元格信息?有什么建议吗?

我已经通过将单元格信息存储到singleton类中自行解决了这个问题。

class Manager {
public static let shared = Manager()

public var ratingSelectedText = String()
public var textBoxSelectedText = String()
public var ratingWithTextboxText = String()
public var textWithCheckBoxSelectedText = String()
public var radioButtonsSelectedText = String()
public var textWithTextBoxText = String()
public var yesNoSelectedText = String()

}

并将它们存储在相应的单元格中,并在提交按钮操作中访问它们。现在,即使在细胞之外,工作也如预期的那样良好。

感谢所有提出建议的人。

最新更新