ios - 热衷于使用 UITableViewCell 中的数据制作 UIAlert?

标签 ios swift uitableview uialertview

我有带有 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/

相关文章:

ios - UITableView 不填充数据

swift - 无法访问 fts4 虚拟表 - SQLite 数据库错误 : 1 "no such table:"

ios - Tableview 和自定义单元格不可预测的行为

swift - 设置 UITableView 标题 View 高度取决于其 subview

ios - 如何将 UISegmentedControl 添加到带有边距的 UITableView 的部分标题?

ios - 如何使用 CKSubscription 检查 CKAsset 的变化?

ios - 如何在 iOS 上拍摄全屏相机照片?

ios - 隐藏大写锁定图标 Xamarin iOS

ios - 将 subview 内的对象固定到位

swift - 在 swift 中假设集合类型时类型推断是如何工作的