TableViewで要素を並び替えをしたいです。例としては以下のリンクのような事。
こういったModelを並べ替える
[ [id: 0,name: "猫"], [id: 1,name: "犬"], [id: 2,name: "猿"], [id: 3,name: "キジ"], [id: 4,name: "トラ"] ]
http://qiita.com/hirofumi_05/items/4a476f6861d921832b89
力技で一旦全削除して入れ直すなど終わらせるためだけの方法は思いつくのですが
もう少し簡単なベストプラクティスをご教授のほどよろしくお願いいたします。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答1件
0
順番を表すプロパティ(例えばorder
)を追加すればいいんじゃないでしょうか?
[ [id: 0, order: 2, name: "猫"], [id: 1, order: 0, name: "犬"], [id: 2, order: 4, name: "猿"], [id: 3, order: 1, name: "キジ"], [id: 4, order: 3, name: "トラ"] ]
中央値を取る方法
Swift2の丸投げコードです。
セルはRight Detailで。
swift
1class TableViewController: UITableViewController { 2 3 var array = [ 4 ["id": 0, "order": 2.0, "name": "猫"], 5 ["id": 1, "order": 0.0, "name": "犬"], 6 ["id": 2, "order": 4.0, "name": "猿"], 7 ["id": 3, "order": 1.0, "name": "キジ"], 8 ["id": 4, "order": 3.0, "name": "トラ"], 9 ] 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 self.navigationItem.rightBarButtonItem = self.editButtonItem() 14 self.sortArray() 15 } 16 17 func sortArray() { 18 array = array.sort{($0["order"] as! Double) < ($1["order"] as! Double)} 19 } 20 21 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 22 return array.count 23 } 24 25 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 26 let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) 27 let obj = array[indexPath.row] 28 cell.detailTextLabel!.text = "\(obj["order"]!)" 29 cell.textLabel!.text = obj["name"] as? String 30 return cell 31 } 32 33 override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { 34 35 print("from \(fromIndexPath.row) to \(toIndexPath.row)") 36 37 if fromIndexPath.row == toIndexPath.row { 38 print("skip") 39 } else { 40 var newOrder: Double 41 if toIndexPath.row == 0 { 42 //一番上に移動した場合は先頭セル - 1 43 newOrder = (array[toIndexPath.row]["order"] as! Double) - 1.0 44 } else if toIndexPath.row == array.count - 1 { 45 //一番下に移動した場合は末尾セル + 1 46 newOrder = (array[toIndexPath.row]["order"] as! Double) + 1.0 47 } else { 48 //途中に移動したときは上下セルの中央値 49 var rowPrev: Int, rowNext: Int; 50 if (toIndexPath.row < fromIndexPath.row) { 51 //上に移動した場合 52 rowPrev = toIndexPath.row - 1 53 rowNext = toIndexPath.row 54 } else { 55 //下に移動した場合 56 rowPrev = toIndexPath.row 57 rowNext = toIndexPath.row + 1 58 } 59 newOrder = ((array[rowPrev]["order"] as! Double) + (array[rowNext]["order"] as! Double)) / 2.0 60 } 61 array[fromIndexPath.row]["order"] = newOrder 62 63 self.sortArray() 64 tableView.reloadData() 65 } 66 } 67}
投稿2017/04/13 00:47
編集2017/04/19 02:03総合スコア16731
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/13 00:54
2017/04/18 07:31 編集
2017/04/18 07:06
2017/04/18 08:01
2017/04/18 08:53
2017/04/18 09:23
2017/04/19 02:04
2017/04/19 13:01
2017/04/19 16:32
2017/04/20 01:55
2017/04/20 02:08 編集
2017/04/20 02:30 編集
2017/04/20 02:36 編集
2017/04/20 02:36
2017/04/20 02:37