为每个tableview实体触发单独的segue

时间:2018-12-18 18:47:04

标签: swift tableview segue

我有一个问题,关于触发单个选择并将用户生成的数据存储在每个tableView实体(患者)中。我要添加的信息对于每个患者都是相同的。但是我有一个问题,一个病人的信息对其他病人仍然可见。

导入UIKit 导入RealmSwift

PatienIDViewController类:UITableViewController {

let realm = try! Realm()



var itemArray: Results<Item>?







override func viewDidLoad() {

    super.viewDidLoad()




    //// Nicht vergessen

    loaditems()



    //            // Do any additional setup after loading the view.
}



//Mark - TableView Datasource Methods
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return itemArray?.count ?? 1
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoItemCell", for: indexPath)

    if  let item = itemArray?[indexPath.row] {

        cell.textLabel?.text = item.title

        // Ternary operator
        // value = condition ?

        cell.accessoryType = item.done ? .checkmark : .none

    }else{
        cell.textLabel?.text = "No Patients added"
    }

    return cell

}

//Mark - TableView - Delegate Methods
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
    IndexPath) {




    if let item = itemArray? [indexPath.row] {
        do{
            try realm.write {
                item.done = !item.done
            }
        }catch{
            print ("Error saving done Status, \(error)")
        }
    }

    tableView.reloadData()


    //        print(itemArray[indexPath.row])

    // Accessory


    //        context.delete(itemArray[indexPath.row])
    //        itemArray.remove(at: indexPath.row)
    //        itemArray[indexPath.row].done = !itemArray[indexPath.row].done


    //        save(item: Item)

    tableView.deselectRow(at: indexPath, animated: true)
    // Hier könnte man einen Button einfügen
}




@IBAction func AddButtonPressed(_ sender: UIBarButtonItem) {




    var textField = UITextField()

    let alert = UIAlertController(title: "Add New Patient", message: "", preferredStyle: .alert)

    let action = UIAlertAction(title: "Add Patient", style: .default) { (action) in
        // what will happen once the surgeon clicks the Add Patient Item


        let newItem = Item()


        newItem.title = textField.text!
        //            newItem.dateCreated = Date()
        newItem.done = false


        self.save(item: newItem)

    }

    alert.addTextField { (alertTextField) in
        alertTextField.placeholder = "Add New Patient iD"
        print(alertTextField.text!)
        textField = alertTextField
    }

    alert.addAction(action)
    present(alert, animated: true, completion: nil)


}


func save (item: Item) {


    do {
        try realm.write {
            realm.add(item)
        }
    }catch{
        print ("Error saving context \(error)")
    }




    self.tableView.reloadData()
}

/// Read the Data
func loaditems() {

    itemArray = realm.objects(Item.self)


    tableView.reloadData()
}


override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCell.EditingStyle.delete{
        if let item = itemArray?[indexPath.row] {
            try! realm.write {
                realm.delete(item)
            }
            tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
        }

}

}

}

Picture of the TableView, where the individual entities are stored

1 个答案:

答案 0 :(得分:0)

上述问题措词含糊。但是我假设您在Patient中显示了PatientListViewController的列表,并且当用户点击表行时,您想将单个Patient传递给PatientDetailViewController,而您想通过触发segue来做到这一点。

首先,实现您的UITableViewDelegate:

extension PatientListViewController: UITableViewDelegate {
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let patient = patient(at: indexPath) // Implement this in your class
    performSegue(withIdentifier: "toPatientDetail", sender: patient)
  }
}

然后,将单个患者发送到视图控制器:

class PatientListViewController: UIViewController {
  // ...
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    let destinationPatient = sender as! Patient
    let destination = segue.destination as! PatientDetailViewController
    destination.patient = destinationPatient
  }
  // ...
}