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

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

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

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

Q&A

解決済

2回答

2282閲覧

Swift3.0 Xcode8.1 tableViewで選択した要素の形式がDictionary型の場合の処理

udb

総合スコア25

Swift

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

0グッド

0クリップ

投稿2016/12/12 02:03

編集2016/12/12 02:27

TableViewで要素の並び替えを行うときにデータの形式が辞書の場合に、その列に含まれている要素を指定すると、辞書型ではソースの列を指定できないとエラーが出ます。

この場合、どのような形式を指定すればエラーが解消されるのでしょうか?
ご指導をよろしくお願い宜しくお願い致します。

swift

1//空の辞書を定義 2var items = [Dictionary<String, String>]() 3 4 func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { 5 // データの順番を整える 6 let targetTitle = items[sourceIndexPath.row] 7//この行にエラーがでる 8 if let index = items.index(of: targetTitle) { 9 items.remove(at: index) 10 items.insert(targetTitle, at: destinationIndexPath.row) 11 print(items) 12 } 13

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

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

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

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

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

fuzzball

2016/12/12 02:23

コードは ``` で囲って下さい。
guest

回答2

0

ベストアンサー

データはArrayになっていますが中身がDictionaryなのでちょっと工夫が必要ですね。

検証したコードは以下になります。
データの構造はシンプルなものなのでご自身が定義しているものに置換えて使用してください。

swift

1 2import UIKit 3 4class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 5 6 @IBOutlet weak var tableView: UITableView! 7 var items: [Dictionary<String, String>] = [] 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 12 items = [["title": "a"], ["title": "b"], ["title": "c"], ["title": "d"], ["title": "e"], ["title": "f"]] 13 14 tableView.estimatedRowHeight = 20 15 tableView.rowHeight = UITableViewAutomaticDimension 16 17 navigationItem.leftBarButtonItem = editButtonItem 18 } 19 20 override func setEditing(_ editing: Bool, animated: Bool) { 21 super.setEditing(editing, animated: animated) 22 tableView.isEditing = editing 23 } 24 25 func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { 26 return true 27 } 28 29 func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { 30 31 let target = items[sourceIndexPath.row] 32 let targetTitle = items[sourceIndexPath.row]["title"]! 33 if let index = items.index(where: { $0["title"] == targetTitle }) { 34 35 items.remove(at: index) 36 items.insert(target, at: destinationIndexPath.row) 37 } 38 } 39 40 41 // MARK: - TableView Delegate & DataSource 42 43 // Row Count 44 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 45 return items.count 46 } 47 48 // Generate Cell 49 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 50 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) 51 cell.textLabel?.text = items[indexPath.row]["title"] 52 return cell 53 } 54 55 // Select Cell 56 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 57 tableView.deselectRow(at: indexPath as IndexPath, animated: true) 58 print("Cell Tap - ",indexPath.row) 59 } 60}

投稿2016/12/12 22:27

_Kentarou

総合スコア8490

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

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

udb

2016/12/15 05:30

検証ありがとうございます。こちらでも検証させて頂いて返事が遅れましたことをお詫び申し上げます。 辞書型は他の形式に比べて複雑になるのですね。非常に勉強になりました。
guest

0

Arrayにして下さい。

投稿2016/12/12 02:25

fuzzball

総合スコア16731

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問