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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode 7

Xcode 7は、ソフトウェア開発のためのアップルの統合開発環境であるXcodeのバージョン。UIを作成するために用いるグラフィカルツールです。iOS9/OS X El Capitan/watchOS2に対応。Swift 2コンパイラーが搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

App Store

App Storeは、Apple社が運営する、iPhone、iPod touch、iPad向けアプリケーションソフトのダウンロードサービスです。携帯電話、Wi-Fiによる無線通信に対応しており、多くのアプリケーションをダウンロード、インストールすることができます。世界中の開発者によってアプリケーションが登録されており、有償のソフトもあればフリーソフトも多く登録されています。

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

Q&A

1回答

2392閲覧

Swift TableViewCellの削除できない

Jared

総合スコア15

Xcode 7

Xcode 7は、ソフトウェア開発のためのアップルの統合開発環境であるXcodeのバージョン。UIを作成するために用いるグラフィカルツールです。iOS9/OS X El Capitan/watchOS2に対応。Swift 2コンパイラーが搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

App Store

App Storeは、Apple社が運営する、iPhone、iPod touch、iPad向けアプリケーションソフトのダウンロードサービスです。携帯電話、Wi-Fiによる無線通信に対応しており、多くのアプリケーションをダウンロード、インストールすることができます。世界中の開発者によってアプリケーションが登録されており、有償のソフトもあればフリーソフトも多く登録されています。

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

0グッド

0クリップ

投稿2015/11/17 14:50

TableViewでスワイプまたは、EditModeで削除・追加する方法を試行錯誤しています。
現状ですと、追加もできておりませんし、Deleteを押したら固まったまま動かなくなります。

知識のあるかた、ご教授よろしくお願いいたします。

swift

1import UIKit 2 3 4 5 6class example: UITableViewController{ 7 8 9 @IBOutlet var tableview: UITableView! 10 11 12 struct Objects { 13 var sectionName : String! 14 var sectionObjects : [String]! 15 } 16 17 var objectsArray = [Objects]() 18 19 20 21 override func viewDidLoad() { 22 super.viewDidLoad() 23 24 25 navigationItem.rightBarButtonItem = editButtonItem() 26 27 28 objectsArray = [Objects(sectionName: "A", sectionObjects: ["aaa", "aaaa", "aaaaa"])] 29 30 31 32 // Do any additional setup after loading the view. 33 34 } 35 36 //------------------------------------------- 37 38 39 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 40 let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell! 41 42 cell.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row] 43 44 return cell 45 } 46 47 48 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 49 return objectsArray[section].sectionObjects.count 50 } 51 52 53 override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 54 return objectsArray.count 55 } 56 57 override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 58 return objectsArray[section].sectionName 59 } 60 61 62 //----------------------------------- 63 64 65 override func setEditing(editing: Bool, animated: Bool) { 66 super.setEditing(editing, animated: animated) 67 tableView.editing = editing 68 } 69 70 override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 71 return true 72 } 73 74 override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 75 76 objectsArray.removeAtIndex(indexPath.row) 77 78 tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: indexPath.row, inSection: 0)], 79 withRowAnimation: UITableViewRowAnimation.Fade) 80 } 81 82 83 84 85 override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { 86 return true 87 } 88 89 90 //------------------------------------ 91 92 93 override func didReceiveMemoryWarning() { 94 super.didReceiveMemoryWarning() 95 // Dispose of any resources that can be recreated. 96 97 98 } 99} 100 101

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

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

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

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

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

guest

回答1

0

UITableViewControllerは既にtableView: UITableViewがpropartyとして宣言されているので
exampleのクラスにtableview: UITableViewは不要です。

クラッシュの原因は、セルの削除を実行しようとした時にセクションごと削除しているためです。
ご自分のobjectsArrayのデータ構造を今一度よく見直してみてください。

Swift

1override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 2 3 // これはセクションをまるごと削除している 4 // objectsArray.removeAtIndex(indexPath.row) 5 6 // セルひとつだけを削除する 7 objectsArray[indexPath.section].sectionObjects.removeAtIndex(indexPath.row) 8 9 // objectArrayに対して編集したindexPathと描画を更新するindexPathは一致するので再生成する必要なし 10 tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade) 11}

投稿2015/11/18 02:40

TakutoNagano

総合スコア150

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問