発生している問題
オプションボタンをCustomCell上に設置し、そこから指定した配列の削除までは実施する事が出来のですが、アニメーションを付けて削除したいです。
1. func deleteButtonメソッド内で、
swift.ViewController
1self.tableView.deleteRows(at: [indexPath], with: .automatic)
で削除したいのですが、エラー
Cannot find 'indexPath' in scope
それで、
swift.ViewController
1self.tableView.deleteRows(at: [self.tableView.indexPath], with: .automatic)
でindexPatnを指定してあげればいいのかなと思ったのですが、エラー
Cannot convert value of type '(UITableViewCell) -> IndexPath?' to expected element type 'Array<IndexPath>.ArrayLiteralElement' (aka 'IndexPath')
が出ます。(エラー文の意味がよくわからないです)
次にどうしたらいいのか解らずに参ってしまっています。
該当のソースコード
以下コード全文です。
swift.ViewController
1import UIKit 2 3class ViewController: UIViewController { 4 5 var itemArray = ["cell 1","cell 2","cell 3"] 6 7 @IBOutlet weak var tableView: UITableView! 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 11 //tableViewの設定を記述 12 tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "customCell") 13 tableView.delegate = self 14 tableView.dataSource = self 15 } 16} 17 18extension ViewController: UITableViewDelegate,UITableViewDataSource { 19 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 20 itemArray.count 21 } 22 23 24 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 25 //tableViewの内容を指定 26 let cell = tableView.dequeueReusableCell(withIdentifier: "customCell",for: indexPath) as! TableViewCell 27 //忘れやすいので注意 28 cell.content.text = itemArray[indexPath.row] 29 cell.indexPathRow = indexPath.row 30 cell.delegate = self 31 return cell 32 } 33} 34extension ViewController: TableViewCellDelegate { 35 36 func deleteButton(indexPathRow: Int) { 37 let alert: UIAlertController = UIAlertController(title: "注意", message: "削除してもいいですか?", preferredStyle: .alert) 38 let canselAction: UIAlertAction = UIAlertAction(title: "キャンセル", style: .cancel) { (UIAlertAction) in 39 print("キャンセル") 40 } 41 let okAction: UIAlertAction = UIAlertAction(title: "削除", style: .destructive) { (UIAlertAction) in 42 43 self.itemArray.remove(at: indexPathRow) 44// コードの書き方がわからない!! 45// self.tableView.beginUpdates() 46//. self.tableView.deleteRows 47// self.tableView.endUpdates() 48 self.tableView.reloadData() 49 50 51 print(self.itemArray) 52 } 53 alert.addAction(okAction) 54 alert.addAction(canselAction) 55 present(alert, animated: true, completion: nil) 56 } 57}
swift.TableViewCell
1import UIKit 2 3protocol TableViewCellDelegate { 4 func deleteButton(indexPathRow: Int) 5} 6 7class TableViewCell: UITableViewCell { 8 9 var delegate: TableViewCellDelegate? 10 var indexPathRow = 0 11 12 @IBOutlet weak var content: UILabel! 13 @IBAction func deleteButton(_ sender: Any) { 14 delegate?.deleteButton(indexPathRow: indexPathRow) 15 } 16 override func awakeFromNib() { 17 super.awakeFromNib() 18 // Initialization code 19 } 20 21 override func setSelected(_ selected: Bool, animated: Bool) { 22 super.setSelected(selected, animated: animated) 23 24 // Configure the view for the selected state 25 } 26}
以下の画像はCustomCellの表示内容です。
回答1件
あなたの回答
tips
プレビュー