2017-08-15 12 views
16

私のタイトルは、UITableViewの最初の行をスワイプしたいと言っているときに、ユーザがそれに来るときにViewControllerとなるでしょう。スワイプタッチなしのUITableViewCell

私のViewController私は1つのUITableViewを持っています、各行には2つのボタン "More"と "Delete"アクションがあります。コードを見てください

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] 
} 

すべてがうまくいきます。しかし、エンドユーザが初めてこのViewControllerに来たときに、スワイプアクションが利用可能であることを通知するので、彼らがそれを実行するように通知します。

質問:最初の行で自動的に左右にスワイプするにはどうすればよいですか?

私は何をしましたか?

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 
     }) 
    } 
} 

上記のコードでは、スワイプ/移動セルは機能していますが、「削除」と「詳細」ボタンは表示されません。

正しい方向に案内してください。

+0

あなたのアプローチは左右にスワイプするのが正しいようです。しかし、組み込みのボタンをそのように強調表示できるかどうかはわかりません。確かにできることは、コンテンツの下にネイティブコントロールのようなUIViewを配置し、スワイプして表示し、セルから削除/非表示にすることです。この回答を確認してください:https://stackoverflow.com/questions/5301728/is-it-possible-to-programmatically-show-the-red-delete-button-on-a-uitableviewce – dirtydanee

+0

この古いフレームワークを使用することができます: https://github.com/CEWendel/SWTableViewCell。次にviewdidappearに書き込みます。 [cell showRightUtilityButtonsAnimated:YES]; –

+0

それでは、最初の行にスワイプを自動的にさせて、編集ボタンが見えるようにすることを目指しています。そうですか? –

答えて

6

私はそれを達成するための一つの方法を発見しました。

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() 
      }) 

     } 
    } 
} 

セルの末尾にカスタムUIViewを追加して、アニメーションが完了した後、それを削除します。

  • あなたは
  • たいとあなたが好きな背景色とタイトルとラベル(複数可)を追加するカスタムビューの枠を設定することができます。

このコードを呼び出すたびにコードを書く必要があります。このコードをviewDidAppearで呼び出す必要があります。これは、ユーザーの指示のみで一度だけ呼び出す必要があります。

+0

私はそれが私を助けることをあなたの論理の希望を試してみます。 – Johnty

+0

ありがとうございました:)私は、私が望むように完璧に努力しました。ありがとうございました。 – Johnty

+0

@Johntyあなたを助けてうれしい:) – Govaadiyo

1

セルを左に変換しても、アクションボタンは表示されません。

あなたのviewDidAppearでこれを入れて、セルにsetEditingメソッドを呼び出すことにより、編集モードにそのセルを送信する必要があります。

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

試しましたが動作しません。 – Johnty

関連する問題