我有带有 tableView
的转换器应用程序,其中包含包含转换结果的单元格。
例如,您输入了 5 公里,在 tableView
单元格中的“米”显示为 5000 等等。
我想在按下其中一个单元格时使 UIAlert
具有完整结果。
所以我想查看米的结果,然后按米单元格。我怎样才能做到呢?
我尝试创建额外的变量并将它们连接到警报,但该值仍然显示不是我选择的单元格,而是当前显示的表格中的第一个单元格...
有人有想法吗?
<小时/>let cell1 = tableView.dequeueReusableCell(withIdentifier: "resultCell") as! ResultTableViewCell
let item = converter[indexPath.row]
cell1.nameResult.text = item.title
cell1.subtitleResult.text = item.subtitle
//MARK: - Форматтер для результатов
let formatter = NumberFormatter()
formatter.locale = Locale.current
formatter.numberStyle = .decimal
formatter.minimumFractionDigits = 3
formatter.maximumFractionDigits = maxdigits
if converterVal == NSLocalizedString("Километр", comment: "") {
cell1.labelResult.text = formatter.string(from: NSDecimalNumber(value: inputValue).multiplying(by: NSDecimalNumber(value: item.kfKilometer)))
resultVar = cell1.labelResult.text!
resultName = cell1.nameResult.text!
return cell1
} else if converterVal == NSLocalizedString("Метр", comment: "") {
cell1.labelResult.text = formatter.string(from: NSDecimalNumber(value: inputValue).multiplying(by: NSDecimalNumber(value: item.kfMeter)))
resultVar = cell1.labelResult.text!
resultName = cell1.nameResult.text!
return cell1
} else {
cell1.labelResult.text = formatter.string(from: NSDecimalNumber(value: 0))
return cell1
}
<小时/>
nameResult
- 是转换值的名称
和 labelResult
- 是转换的结果
对于每个单元格,我都有 if else
语句。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView == self.tableView2 {
let alertController = UIAlertController(title: NSLocalizedString("\(inputValue) \(detailLabel) is:", comment: ""), message: NSLocalizedString("\(resultVar) \(resultName)", comment: ""), preferredStyle: .alert)
let OKAction = UIAlertAction(title: NSLocalizedString("Спасибо", comment: ""), style: .default) { action in
// ...
}
alertController.addAction(OKAction)
self.present(alertController, animated: true) {
// ...
}
}
// tableView.deselectRow(at: indexPath, animated: true)
}
<小时/>
我想在警报中更仔细地显示这个标签,但仅对于每个单元格来说,它必须是自己的......
最佳答案
可能您需要像下面这样修改您的方法
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView == self.tableView2 {
// get your selected row data
let item = converter[indexPath.row]
//use item.title or item.subtitle instead of label
let alertController = UIAlertController(title: NSLocalizedString("\(inputValue) \(detailLabel) is:", comment: ""), message: NSLocalizedString("\(resultVar) \(resultName)", comment: ""), preferredStyle: .alert)
let OKAction = UIAlertAction(title: NSLocalizedString("Спасибо", comment: ""), style: .default) { action in
// ...
}
alertController.addAction(OKAction)
self.present(alertController, animated: true) {
// ...
}
}
}
让我解释一下:cellForRowAt indexPath
此方法创建单元格并填充您传入的任何数据。因此,如果您在此方法中分配值resultVar
,它将给您错误的值。
didSelectRowAt
当您单击单元格行时调用,因此在此方法中分配值 resultVar
并执行您的操作或任何您想要的操作
当您单击行时访问单元格
if let cell = tableView.cellForRow(at: indexPath) as? YourCellClass {
// access your cell label and get values
print(cell.titleLable.text!)
}
关于ios - 热衷于使用 UITableViewCell 中的数据制作 UIAlert?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52567144/