realm selectedRowAtIndexPath使用segue显示不同的结果。

问题描述 投票:1回答:1

这是我第一次使用段控制和领域。所以目前我使用segue来执行segue到AddEditVC来修改数据。

当segue执行并把数据带到AddEditVC到变量selectedTransaction时,我想相应地填充以下数据。

/* selectedTransaction print statement Results */
print("selectedTransaction = \(selectedTransaction!)")

selectedTransaction = Transaction {
    categoryType = Category {
        type = expense;
        name = EXPENSE 3;
    };
    amount = 1000;
    date = April 2;
}

  1. amountTF.text = selectedTransaction.金额 (已完成并改正)
  2. categorySCoutlet.selectedSegmentIndex = selectedTransaction.categoryType.type。(未能显示selectedTransaction.categoryType.type处的segmentindex)
  3. categoryTF.text = selectedTransaction.categoryType.name。(名称显示正确,但如果用户没有再次RESELECT,将返回nil)

我希望所有的数据都能显示出来,就像它的创建一样。

但是,我在做的过程中遇到了2个问题。

  1. SegmentControl选择的索引总是在0而不是selectedTransaction.categoryType.name中(我希望selectedTransaction.categoryType.name在创建的索引中)。

  2. categoryTF.text显示正确,但如果我没有再次选择,而是保持原样,那么当我按下saveButton时,它将自动返回结果中的nil。我希望categoryTF.text能够恢复到创建的状态,即使我没有触碰它而点击了saveButton,它的值也不会改变。

在下面的gif图中,我选择了row2作为样本,结果在Realm Browser中显示为原样,我只是将金额TF.text信息从1200改成了2000,结果Realm Browser会将segmentcontrol中的cateogoryType的结果设置为 "收入",category会返回为nil。

工作流程示例


//Data Model
//MARK: - Transaction Category Section
 enum CategoryType : String, CaseIterable {
     case income = "income"
     case expense = "expense"

     init?(id : Int) {
         if id < CategoryType.allCases.count {
             self = CategoryType.allCases[id]
         } else {
             return nil
         }
     }
 }



class Category : Object {
    @objc dynamic var type : String = CategoryType.income.rawValue
    @objc dynamic var name : String = ""

//    let parentCategory = LinkingObjects(fromType: Transaction.self, property: "ofCategory")
    convenience init(type:CategoryType, name: String) {
        self.init()
        self.type = type.rawValue
        self.name = name
    }

}
/* VC that should read and load all data to required place */

 //edit
    var selectedTransaction : Transaction!
 @IBOutlet weak var amountTF: UITextField!

    //PickerView for keyboard
    lazy var pickerView : UIPickerView = UIPickerView()
    //Segment Control
    @IBOutlet weak var categorySCoutlet: UISegmentedControl!
    @IBOutlet weak var categoryTF: UITextField!

 override func viewDidLoad() {
        super.viewDidLoad()
        amountTF.text! = selectedTransaction.amount
        categoryTF.text! = selectedTransaction.categoryType!.name

setupPicker()

    }

    @IBAction func categoryTypeSC(_ sender: UISegmentedControl) {
        guard let type = CategoryType(id: sender.selectedSegmentIndex) else {
            fatalError("error")
        }
        currentCategories = categories.filter("type == %@", type.rawValue)
        categoryTF.text = currentCategories.first?.name
        pickerView.reloadAllComponents()
        pickerView.selectRow(1, inComponent: 0, animated: true)
    }

 //MARK:- Add Transaction Btn
    @IBAction func addTransButtonTapped(_ sender: UIButton) {


    }



    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        if touches.first?.view == view {
            categoryTF.resignFirstResponder()
        }
    }


    //MARK:- Picker Helper
    func setupPicker() {
        currentCategories = categories.filter("type == %@", CategoryType.income.rawValue)
        categoryTF.inputView = pickerView

        pickerView.delegate = self
        pickerView.dataSource = self

        categorySCoutlet.setTitle("Income", forSegmentAt: 0)
        categorySCoutlet.setTitle("Expense", forSegmentAt: 1)
        categorySCoutlet.addTarget(self, action: #selector(categoryTypeSC(_:)), for: .valueChanged)

    }
swift realm segue uisegmentedcontrol
1个回答
0
投票

你正在调用 tableView.deselectRow(at: indexPath, animated: false) 在你 tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 方法。

这将导致返回错误的 indexPathprepareForSegue. 你应该删除 deselectRow 来自 didSelectRowAtIndexPath 方法,或者你应该创建一个属性来保存 indexPath. 类似这样

// somewhere in your class
var selectedIndexPath: IndexPath?

然后

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let selectedRowForTrans : Transaction = getTransactions[indexPath.row]
    selectedIndexPath = indexPath // set the value of selectedIndexPath
    tableView.deselectRow(at: indexPath, animated: false)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   if (segue.identifier == "editTrans") {
        if let indexPath = selectedIndexPath { // check if there is a valid indexPath
            let editTransVC = segue.destination as! AddTransactionTableViewController
            let selectedRowForTrans : Transaction = getTransactions[indexPath.row]
            editTransVC.selectedTransaction = selectedRowForTrans

        }
    }

}

希望能帮到你

© www.soinside.com 2019 - 2024. All rights reserved.