質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

523閲覧

CustomCellをアニメーション付きで削除したい!!

masaboy

総合スコア61

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2022/03/01 19:46

発生している問題

オプションボタンを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の表示内容です。

イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

masaboy

2022/03/01 20:35

5日考えても全然わからなかったので、本当にありがとうございます!! 感謝です。
guest

回答1

0

自己解決

func deleteButtonメソッド内に

swift.ViewController

1let indexPath = IndexPath(row: indexPathRow, section: 0)

を追記

投稿2022/03/01 20:40

masaboy

総合スコア61

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問