简体   繁体   中英

Hot to make UIAlert with data from UITableViewCell?

I have converter application with tableView with cells which contains results of conversion.

For example you entered 5 kilometers and in tableView cells for Meters shows 5000 and so on.

I want to make UIAlert with full result when press to one of cells. So i want to se my result for Meters and i press on meters cell. How could i make it?

I tried to make a additional variables and connect them to Alert but the value still shows not the cell that i choose but the first cell at the table that shows at the moment...

Does anyone have ideas?


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 - is name of converted value and labelResult - is result of conversion and for every cell i have if else statement.


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)

}

And i want to show more closely this labels in Alert but only for every cell it must be own...

May be you need to modify you method like below

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) {
        // ...
    }
    }

}

Let me explain something : cellForRowAt indexPath this method create cell and fill data whatever you pass in it. so if you assign value resultVar in this method, it will give you wrong value.

didSelectRowAt called when you click on cell row, so assign value resultVar in this method and do your operations or whatever you want

Access cell instead when you click on row

if let cell = tableView.cellForRow(at: indexPath) as? YourCellClass {
     // access your cell label and get values
     print(cell.titleLable.text!)

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM