[英]Hot to make UIAlert with data from UITableViewCell?
我有带有tableView
转换器应用程序,其中的单元格包含转换结果。
例如,您输入了5公里,并且在tableView
的Meters单元格中显示5000,依此类推。
我想使UIAlert
具有完整的结果,当按到单元格之一时。 因此,我想查看我的仪表结果,然后按一下仪表单元。 我该怎么做?
我尝试制作其他变量并将其连接到Alert,但该值仍未显示我选择的单元格,而是当前显示的表中的第一个单元格...
有人有想法吗?
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)
}
我想在Alert中更紧密地显示此标签,但仅对于每个单元格它都必须是自己的...
可能是您需要修改如下的方法
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
在此方法中分配值resultVar
并执行操作或所需的任何操作
单击行时访问单元格
if let cell = tableView.cellForRow(at: indexPath) as? YourCellClass {
// access your cell label and get values
print(cell.titleLable.text!)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.