2017-08-15 69 views
16

Jako tytuł mówiący, że chcę przesunąć pierwszy wiersz UITableView od lewej do prawej, gdy użytkownik przyjdzie na tym ViewController.Swipe UITableViewCell bez dotyku

W moim ViewController Mam jeden UITableView, każdy wiersz ma dwa przyciski "Więcej" i "Usuń" akcję. Spójrz na poniższy kod:

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
    return true 
} 

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if (editingStyle == UITableViewCellEditingStyle.delete) { 
    } 
} 


func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 
    let deleteButton = UITableViewRowAction(style: .normal, title: "Delete") { action, index in 
     // Edit Button Action 
    } 
    deleteButton.backgroundColor = UIColor.red 

    let editButton = UITableViewRowAction(style: .normal, title: "Edit") { action, index in 
     // Delete Button Action 
    } 
    editButton.backgroundColor = UIColor.lightGray 

    return [deleteButton, editButton] 
} 

Wszystko działa dobrze. Ale chcę, aby użytkownik końcowy pojawił się na tym ViewController po raz pierwszy, więc powiadomi, że dostępna jest operacja machnięcia, więc wykonają ją.

Pytanie brzmi: Jak mogę przesunąć automatycznie w lewo i w prawo dla pierwszego rzędu?

Co zrobiłem?

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 
    let cell = posTblView.cellForRow(at: NSIndexPath(row: 0, section: 0) as IndexPath) as! POSUserTabelCell 
    UIView.animate(withDuration: 0.3, animations: { 
     cell.transform = CGAffineTransform.identity.translatedBy(x: -150, y: 0) 
    }) { (finished) in 
     UIView.animateKeyframes(withDuration: 0.3, delay: 0.25, options: [], animations: { 
      cell.transform = CGAffineTransform.identity 
     }, completion: { (finished) in 
     }) 
    } 
} 

Powyższy kod przesuwa palcem/poruszającą się komórką, ale nie wyświetla przycisku "Usuń" i "Więcej".

Proszę, poprowadź mnie we właściwym kierunku.

+0

Twoje podejście wydaje się być odpowiednie do przesuwania w lewo i prawo. Jednak nie jestem pewien, czy wbudowane przyciski mogą być podświetlone w ten sposób. To, co możesz zrobić na pewno, to umieścić "UIView" wyglądające jak natywne elementy sterujące poniżej zawartości, przesuń palcem, aby je odsłonić, a następnie usuń/ukryj je z komórki. Sprawdź tę odpowiedź: https://stackoverflow.com/questions/5301728/is-it-possible-to-programmatically-show-the-red-delete-button-on-a-uitableviewce – dirtydanee

+0

Możesz użyć tej starej struktury: https://github.com/CEWendel/SWTableViewCell. Następnie w viewdidappear napisz: [cell showRightUtilityButtonsAnimated: YES]; –

+0

W związku z tym celem jest automatyczne przesunięcie pierwszego rzędu, aby można było zobaczyć przyciski edycji. Czy to prawda? –

Odpowiedz

6

Znalazłem jeden sposób, aby to osiągnąć.

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 

    if arrStudent.count > 0 { 
     let indexPath = NSIndexPath(row: 0, section: 0) 
     let cell = tblStudent.cellForRow(at: indexPath as IndexPath); 

     let swipeView = UIView() 
     swipeView.frame = CGRect(x: cell!.bounds.size.width, y: 0, width: 170, height: cell!.bounds.size.height) 
     swipeView.backgroundColor = .clear 

     let swipeEditLabel: UILabel = UILabel.init(frame: CGRect(x: 0, y: 0, width: 80, height: cell!.bounds.size.height)) 
     swipeEditLabel.text = "Edit"; 
     swipeEditLabel.textAlignment = .center 
     swipeEditLabel.font = UIFont.systemFont(ofSize: 19) 
     swipeEditLabel.backgroundColor = UIColor(red: 180/255, green: 180/255, blue: 180/255, alpha: 1) // Light Gray: Change color as you want 
     swipeEditLabel.textColor = UIColor.white 
     swipeView.addSubview(swipeEditLabel) 

     let swipeDeleteLabel: UILabel = UILabel.init(frame: CGRect(x: swipeEditLabel.frame.size.width, y: 0, width: 90, height: cell!.bounds.size.height)) 
     swipeDeleteLabel.text = "Delete"; 
     swipeDeleteLabel.textAlignment = .center 
     swipeDeleteLabel.font = UIFont.systemFont(ofSize: 19) 
     swipeDeleteLabel.backgroundColor = UIColor(red: 255/255, green: 41/255, blue: 53/255, alpha: 1) // Red color: Change color as you want 
     swipeDeleteLabel.textColor = UIColor.white 
     swipeView.addSubview(swipeDeleteLabel) 

     cell!.addSubview(swipeView) 

     UIView.animate(withDuration: 0.60, animations: { 
      cell!.frame = CGRect(x: cell!.frame.origin.x - 170, y: cell!.frame.origin.y, width: cell!.bounds.size.width + 170, height: cell!.bounds.size.height) 
     }) { (finished) in 
      UIView.animate(withDuration: 0.60, animations: { 
       cell!.frame = CGRect(x: cell!.frame.origin.x + 170, y: cell!.frame.origin.y, width: cell!.bounds.size.width - 170, height: cell!.bounds.size.height) 

      }, completion: { (finished) in 
       for subview in swipeView.subviews { 
        subview.removeFromSuperview() 
       } 
       swipeView.removeFromSuperview() 
      }) 

     } 
    } 
} 

Dodaj zwyczaj UIView na koniec komórki i usunąć go po animacja jest wykonywana.

  • Można ustawić ramkę widoku niestandardowego za ile chcesz
  • Dodaj wytwórnię (ów) z kolorowym tłem i tytuł, jak chcesz.

Tutaj należy wpisać kod, aby nie za każdym razem wywoływać ten kod w numerze viewDidAppear, powinien on być wywoływany tylko raz dla wskazania użytkownika.

+0

Spróbuję twojej logiki, mam nadzieję, że to mi pomoże. – Johnty

+0

Dziękuję bardzo :) Próbowałem i pracowałem idealnie, jak chcę. Dzięki agin mężczyzn. – Johnty

+0

@Johnty cieszę się, że mogę Ci pomóc :) – Govaadiyo

1

Przekształcenie komórki w lewo nie spowoduje wyświetlenia przycisku akcji.

Musisz wysłać tę komórkę w tryb edycji za pomocą metody setEditing na komórce, umieścić to w swoim viewDidAppear:

let cell = posTblView.cellForRow(at: IndexPath(row: 0, section: 0)) 
cell.setEditing(true, animated: true) 
+0

Próbowałem, ale nie działa. – Johnty