2016-05-28 18 views
6

Tôi đã tự hỏi nếu có một cách thích hợp để hủy vuốt sang trái trên ô xem bảng sao cho nó trượt lại để ẩn các nút. Tôi không thực sự chắc chắn làm thế nào để nói đúng, lol. Nhưng vui lòng xem GIF bên dưới. Trong GIF đầu tiên, tôi không có mã sau khi nút hủy được nhấn và các nút vẫn hiển thị.Hủy trượt sang trái trên ô xem bảng khi hiển thị thêm các nút trong swift

enter image description here

Ý tưởng duy nhất tôi có là để tải lại các tế bào với mã này self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) Nhưng điều này mang lại một cái nhìn của các nút chuyển lên, trong khi tôi muốn nó xuất hiện sự thay đổi tế bào ủng hộ vào vị trí cho đúng. Xem GIF tải lại bên dưới.

enter image description here

Làm thế nào nên tôi được làm điều này đúng cách? Xem bên dưới để biết mã của tôi có thêm các nút và chức năng của chúng là gì.

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { 

    let edit = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Edit") { 
     (action, indexPath) in 

     if (indexPath == self.indexSelect) { 
      print("Editting Selected Cell") 
     } else { 
      print("Editting a difference cell than selected") 
     } 

     let section: Sections = self.frc.objectAtIndexPath(indexPath) as! Sections 
     let count: Int = self.sectionCount(section.section!) 
     var msg: String? 
     let sectionName: String = section.section! 

     if (count > 0) { 
      msg = "There are \(count) other Items using this Section currently. Editing the Section name \"\(sectionName)\" will affect them all. \n\nThis will be changed immediately!" 
     } 

     let alert = UIAlertController(title: "Edit Section Name", message: msg, preferredStyle: UIAlertControllerStyle.Alert) 
     let editAction = UIAlertAction(title: "Edit", style: UIAlertActionStyle.Destructive) { 
      UIAlertAction in 

      let sectionName = Util.trimSpaces(alert.textFields![0].text!) 

      if (sectionName != section.section) { 
       if (Util.checkSectionName(sectionName, moc: self.moc!) == false) { 
        let entityDesc = NSEntityDescription.entityForName("Sections", inManagedObjectContext: self.moc!) 
        let newSection: Sections = Sections(entity: entityDesc!, insertIntoManagedObjectContext: self.moc) 
        newSection.section = sectionName 

        do { 
         try self.moc!.save() 
        } catch { 
         fatalError("New item save failed") 
        } 

        let oldSection: Sections = section 

        let fetchReq = NSFetchRequest(entityName: "Catalog") 
        let pred = NSPredicate(format: "sections.section == %@", oldSection.section!) 
        fetchReq.predicate = pred 

        do { 
         let results = try self.moc!.executeFetchRequest(fetchReq) 
         for rec in results { 
          let catalog: Catalog = rec as! Catalog 
          catalog.sections = newSection 
         } 

         do { 
          try self.moc!.save() 
         } catch { 
          fatalError("Failed to Save after Delete") 
         } 
        } catch { 
         fatalError("Fetching Items to delete section failed") 
        } 

        self.moc!.deleteObject(oldSection) 

        do { 
         try self.moc!.save() 
        } catch { 
         fatalError("Failed to Save after Delete") 
        } 
       } else { 
        Util.msgAlert("Duplicate Section Name", msg: "\"\(sectionName)\" section name already exists.", curVC: self) 
        self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 
       } 
      } else { 
       self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 
      } 
     } 
     let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { 
      UIAlertAction in 

      //self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 
     } 

     alert.addAction(editAction) 
     alert.addAction(cancel) 
     alert.addTextFieldWithConfigurationHandler { 
      (txtFld) -> Void in 

      txtFld.text = section.section 
      txtFld.autocapitalizationType = UITextAutocapitalizationType.Words 
      txtFld.autocorrectionType = UITextAutocorrectionType.Default 
      txtFld.clearButtonMode = UITextFieldViewMode.WhileEditing 
     } 

     self.presentViewController(alert, animated: true, completion: nil) 
    } 

    edit.backgroundColor = UIColor.init(red: 84/255, green: 200/255, blue: 214/255, alpha: 1) 

    let delete = UITableViewRowAction(style: .Destructive, title: "Delete") { 
     (action, indexPath) in 

     let section: Sections = self.frc.objectAtIndexPath(indexPath) as! Sections 
     let count: Int = self.sectionCount(section.section!) 

     if (count > 0) { 
      let alert = UIAlertController(title: "Confirm Delete", message: "There are \(count) Items using this Section currently. Deleting this Section will reset them all to blank. \n\nThis can't be undone and will take affect immediately!", preferredStyle: UIAlertControllerStyle.Alert) 
      let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { UIAlertAction in } 
      let deleteAction = UIAlertAction(title: "Delete", style: UIAlertActionStyle.Destructive) { UIAlertAction in 
       var blankSection: Sections? 

       var fetchReq = NSFetchRequest(entityName: "Sections") 
       var pred = NSPredicate(format: "section == %@", "") 
       fetchReq.predicate = pred 

       do { 
        let results = try self.moc!.executeFetchRequest(fetchReq) 
        blankSection = (results.first as! Sections) 
       } catch { 
        fatalError("Fetching blank section failed") 
       } 

       fetchReq = NSFetchRequest(entityName: "Catalog") 
       pred = NSPredicate(format: "sections.section == %@", section.section!) 
       fetchReq.predicate = pred 

       do { 
        let group = try self.moc!.executeFetchRequest(fetchReq) 
        for rec in group { 
         let catalog: Catalog = rec as! Catalog 
         catalog.sections = blankSection 
        } 

       } catch { 
        fatalError("Fetching Items to delete section failed") 
       } 

       self.moc!.deleteObject(section) 

       do { 
        try self.moc!.save() 
       } catch { 
        fatalError("Failed to Save after Delete") 
       } 

       if (self.sectionUpdateProtocol != nil) { 
        self.sectionUpdateProtocol!.sectionUpdate(self, section: blankSection!) 
       } 

       //self.navigationController!.popViewControllerAnimated(true) 
      } 

      alert.addAction(deleteAction) 
      alert.addAction(cancel) 

      self.presentViewController(alert, animated: true, completion: nil) 
     } else { 
      self.moc!.deleteObject(section) 

      do { 
       try self.moc!.save() 
      } catch { 
       fatalError("Failed to Save after Delete") 
      } 
     } 

    } 

    return [delete, edit] 
} 
+0

Hãy xem SWTableViewCell hoặc để thực hiện trong hoặc xem cách họ làm điều đó. https://github.com/CEWendel/SWTableViewCell – sschale

Trả lời

6

Chỉ cần gọi:

tableView.setEditing(false, animated: true) 
+0

Ngay trên mũi, đó là những gì tôi muốn. Cảm ơn bạn! –

+0

Mất một lúc để nút xóa biến mất. Có cách nào để biến nó biến mất ngay lập tức không ?. Tôi thậm chí đã cố gắng làm điều trên trong việc xử lý hoàn thành việc loại bỏ cảnh báo, nhưng điều đó cũng không hiệu quả. Tôi thậm chí thiết lập các hình ảnh động để FALSE, mà cũng thất bại –

+0

@zulkarnainshah Tôi biết điều này là muộn, nhưng bạn có thể thử 'tableView.reloadData()' (Thiết lập animate: false, nên đã làm việc mặc dù) –

Các vấn đề liên quan