Swift:获取第一个单元格的值,然后与第二个单元格的值相乘

时间:2018-07-17 11:02:00

标签: ios swift eureka-forms

我正在做一个应用程序表单,我想获取文本字段的值。问题是如何获取第一个单元格的值并与第二个单元格的valueField相乘。
除第一个单元格和结果单元格是尤里卡形式外,其他所有单元格均由Costum单元格制成。

我已经使用了两个文件:

ViewController.swift


import UIKit
import Eureka

class ViewController: FormViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    var rules = RuleSet<String>()

    form +++ Section("EINGABESUMME BRUTTO (EXXL MWST)")

    {
        $0.header?.height = { 20 }

        }
        <<< DecimalRow(){ row in
           //row.value = 0
            row.title = " "
            row.tag = "MyRowTag"
            row.placeholder="0.00"
            row.cell.tag = KalkulationTag.eingabessumeBrutto.rawValue
            }

            .cellUpdate { cell, row in
                cell.textField?.font = .boldSystemFont(ofSize: 18.0)


    }


    form +++ Section("ZIEL-DB PROJEKT BRUTTO")
    {
        $0.header?.height = { 20 }
        }

        <<< KalkulationRow { row in
            //row.tag = "MyRowTag1"
            row.cell.tag = KalkulationTag.zielBrutto.rawValue

            //             row.cell.valueField.text = "54.00"

           // row.value = User(percentage: 45, value: 45)
    }


    form +++ Section("RABATT")
    {
        $0.header?.height = { 20 }

        }
        <<< KalkulationRow { row in
            row.tag = "MyRowTag2"
  //          row.cell.percentageField.tag = KalkulationTag.rabatt.rawValue

            }
        .cellUpdate{cell, row in
            //print("sevgjan")

    }


    form +++ Section("SKONTO")
    {
        $0.header?.height = { 20 }

        }

        <<< IntRow(){
            $0.title = "Tage:"
            $0.cell.tag = KalkulationTag.tage.rawValue
            $0.add(rule: RuleRequired())
            $0.tag = "tage"


            }

            .cellUpdate { cell, row in

                cell.textField?.font = .boldSystemFont(ofSize: 18.0)


        }
        <<< KalkulationRow { row in
         row.cell.valueField.tag = KalkulationTag.skonto.rawValue
            row.cell.percentageField.tag = KalkulationTag.skontoPercent.rawValue
          //row.cell.percentageField.text = "54"
    }
      .cellUpdate { cell, row in
    if cell.tag == KalkulationTag.skonto.rawValue {

        print("rijiljgjd554")
    }
    }
    form +++ Section("DIVERSE ABZUGE")
    {
        $0.header?.height = { 20 }

        }
        <<< KalkulationRow { row in
            //row.cell.valueField.text = "54.00"
            row.cell.tag = KalkulationTag.diverse.rawValue
    }



    form +++ Section("EINGABESSUME NETTO")
    {
        $0.header?.height = { 20 }

        }
        <<< DecimalRow(){
            $0.title = " "
         // $0.value = 0
            $0.placeholder = "0.00"
            $0.placeholderColor = UIColor.green
            $0.cell.tag = KalkulationTag.result.rawValue


            }
            .cellUpdate { cell, row in
                cell.textField?.font = .boldSystemFont(ofSize: 18.0)
                cell.textField?.textColor = UIColor.green
               // cell.textField?.isUserInteractionEnabled = false
    }



    form +++ Section("ZIEL-DB PROJEKT NETTO")
    {
        $0.header?.height = { 20 }

        }

        <<< KalkulationRow { row in
        row.cell.tag = KalkulationTag.zielNetto.rawValue
            //row.cell.valueField.text = "54.00"
    }

}
}

这是KalkulationCell.swift

import Foundation
import Eureka


// You can also map to Int
enum KalkulationTag: Int {
    case zielBrutto = 10
    case rabatt = 120
    case skonto = 11
    case skontoPercent = 43
    case diverse = 12
    case eingabessumeBrutto = 188
    case zielNetto = 14
    case result = 17
    case tage = 122
}


final class KalkulationCell: Cell<User>, CellType , UITextFieldDelegate{



    @IBOutlet var percentageField: UITextField!
    @IBOutlet var valueField: UITextField!

    required init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func setup() {
        super.setup()
        // we do not want our cell to be selected in this case. If you use such a cell in a list then you might want to change this.
        percentageField.delegate = self
        valueField.delegate = self
        selectionStyle = .none


        // 5percentageField.text = "25.00"
        // define fonts for our labels
        percentageField.font = .systemFont(ofSize: 18)
        valueField.font = UIFont.boldSystemFont(ofSize: 18.0)

        percentageField.textColor=UIColor.black
        // percentageField.font = UIFont.boldSystemFont(ofSize: 18.0)
        // specify the desired height for our cell
        //height = { return 94 }

        // set a light background color for our cell
        // backgroundColor = UIColor(red:0.984, green:0.988, blue:0.976, alpha:1.00)
    }

    override func update() {
        super.update()


        // get the value from our row

        guard let user = row.value else { return }
        //
        percentageField.text = user.percentage.description
        valueField.text = user.value.description
    }



    func textFieldDidEndEditing(_ textField: UITextField) {

       calculate()
        //skonto()
        //res()
    }

//    func skonto(){
//        if (self.tag == KalkulationTag.skonto.rawValue) != nil{
//            print("testtt")
//
//        }
//
//
//
//    }
    func res(){
        if (self.tag == KalkulationTag.eingabessumeBrutto.rawValue){
      print("rezultati")

        }
    }

    func calculate(){

        let row: DecimalRow = (formViewController()?.form.rowBy(tag: "MyRowTag"))!

        let perqindja = Double(percentageField.text!)
        let vlera = Double(valueField.text!)
          if let vlera1 = row.value{

                if perqindja != nil {

                    let kosto = (row.value)!*perqindja!*0.01
                    valueField.text  = kosto.description
                    let exclamationMark: Character = "%"
                    percentageField.text?.append(exclamationMark)
                    valueField.isUserInteractionEnabled = false

                }else if vlera != nil {

                    let kosto1 = round((vlera!/(row.value)!)*100)

                    percentageField.text  = kosto1.description
                    let exclamationMark: Character = "%"
                    percentageField.text?.append(exclamationMark)
                    percentageField.isUserInteractionEnabled = false

                }

            }
            else{


            }

    }

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {


        if let text = textField.text,
            !text.isEmpty {
            textField.text?.removeLast()
            //valueField.text?.removeAll()
        }
       calculate()
       // skonto()
        res()
        return formViewController()?.textInputShouldBeginEditing(textField, cell: self) ?? true
    }

}


struct User: Equatable {

    var percentage: String
    var value: String

}
func ==(lhs: User, rhs: User) -> Bool {
    return lhs.percentage == rhs.percentage
}


final class KalkulationRow: Row<KalkulationCell>, RowType {
    required init(tag: String?) {
        super.init(tag: tag)
        cellProvider = CellProvider<KalkulationCell>(nibName: "KalkulationCell")
    }
}

Project

0 个答案:

没有答案