###実現したいこと
MCSwipeTableViewCellを利用して、カスタムセルのスワイプ削除を実装したいです。
###発生している問題・エラーメッセージ
何もセルをカスタマイズしていない状態では、スワイプ操作を確認できたのですが、カスタムセルに対応させる方法がわかりません。
いままで、カスタムセルを利用する際は、
let cell = tableView.dequeueReusableCell(withIdentifier: "todoCell") as! todoListTableViewCell
のように指定しておりましたが、MCSwipeTableViewCellを利用する際は、同じく
tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? MCSwipeTableViewCell
と指定する必要があるかと思います。
カスタムセルとMCSwipeTableViewCellを同時に利用するには、どのような書き方になるのでしょうか。
###該当のソースコード
swift
1import UIKit 2import MCSwipeTableViewCell 3 4class ViewController: UIViewController, UITableViewDataSource { 5 6 var dataList = ["task01","task02", "task03","task04"] 7 @IBOutlet weak var tableView: UITableView! 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 } 12 13 14 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 15 16 // create 17 if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? MCSwipeTableViewCell { 18 19 // setup 20 cell.selectionStyle = .none 21 cell.textLabel?.text = dataList[indexPath.row] 22 cell.defaultColor = .lightGray 23 cell.firstTrigger = 0.25; 24 25 cell.setSwipeGestureWith(UIImageView(image: UIImage(named: "check")), color: UIColor.green, mode: .exit, state: .state1, completionBlock: { [weak self] (cell, state, mode) in 26 27 28 if let cell = cell, let indexPath = tableView.indexPath(for: cell) { 29 self?.dataList.remove(at: indexPath.row) 30 // 該当のセルを削除 31 self?.tableView.deleteRows(at: [indexPath], with: .fade) 32 } 33 }) 34 return cell 35 } 36 return UITableViewCell() 37 } 38 39 func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int { 40 return dataList.count 41 } 42} 43
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/20 08:21