didDeselectRowAtIndexPath赢得了triggr

时间:2016-07-09 23:42:58

标签: swift uitableview cell

我想建立一个编辑联系人组页面。 当我回到编辑页面时,我无法取消选择以前保存的单元格。 didDeselectRowAtIndexPath不会触发。

有什么想法吗? 非常感谢!

import UIKit
import RealmSwift

class SelectJacoTableViewController: UITableViewController, UIActionSheetDelegate {

  // define the data inside the tableView
  var realm = try! Realm()
  var contacts=try! Realm().objects(Contacts.self)
  var Jaco_enabled: [String] = []

  override func viewDidLoad() {
    super.viewDidLoad()

    var enabled_contacts = realm.objects(Contacts.self).filter("Jaco_enabled = true")
    print(enabled_contacts)
    for contact in enabled_contacts {
        Jaco_enabled += [contact.identifier]
    }


    // in order to make a selection of rows
    self.tableView.allowsMultipleSelectionDuringEditing = true
    //self.tableView.allowsMultipleSelection = true

    setEditing(true, animated: false)



    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    //self.navigationItem.rightBarButtonItem = self.editButtonItem()
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }



  override func viewWillAppear(animated: Bool) {
    // Uncomment the following line to preserve selection between presentations

    self.clearsSelectionOnViewWillAppear = false  
  }



// MARK: - Table view data source

  override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
  }

  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    print(contacts.count)
    return contacts.count
  }


  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("JacoContactsList", forIndexPath: indexPath)

    let contact = contacts[indexPath.row]

    let contactNameFormatted = contact.given_name+" "+contact.family_name

    // Configure the cell...

    cell.textLabel!.text = contactNameFormatted


    return cell
  }


  override func tableView(tableView: UITableView,willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath) {

    let enabled_contacts = realm.objects(Contacts.self).filter("Jaco_enabled = true")

    let contact = contacts[indexPath.row]
    if contact.Jaco_enabled {
        print("selected")
        //            cell.setEditing(true, animated: true)
        cell.setSelected(true, animated: true)
        print(enabled_contacts)
        //cell.setEditing(true, animated: true)

    }
    let contactNameFormatted = contact.given_name+" "+contact.family_name
  }


  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


    print("selected")

    let selectedCell = tableView.cellForRowAtIndexPath(indexPath)!
    //selectedCell.backgroundColor = UIColor.purpleColor()
    print(selectedCell)
    let selected_contact=contacts[indexPath.row]
    print(selected_contact)
    let id = selected_contact.identifier
    print(id)
    Jaco_enabled += [id]
    print(Jaco_enabled)

  }

  override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {


    print("deselected")
    var deselectedCell = tableView.cellForRowAtIndexPath(indexPath)!
    deselectedCell.setSelected(false, animated: true)
    print(deselectedCell)
    var deselected_contact=contacts[indexPath.row]
    print(deselected_contact)
    var id = deselected_contact.identifier
    print(id)
    Jaco_enabled = Jaco_enabled.filter() {$0 != id}
    print(Jaco_enabled)
    self.tableView.deselectRowAtIndexPath(indexPath, animated: true)

  } 


  // On the push on the button

  @IBAction func saveJacoContacts(sender: AnyObject) {
    let realm = try! Realm()
    realm.beginWrite()

    self.performSegueWithIdentifier("updateJacoEnabled", sender: self)


    for contact in Jaco_enabled {
        print("yo")
        let filter="identifier = '"+contact+"'" as String

        var full_contact =  try!Realm().objects(Contacts.self).filter(filter).first
            full_contact?.Jaco_enabled = true
    }
    try! realm.commitWrite()

0 个答案:

没有答案
相关问题