###前提・実現したいこと
iosのuitableviewでセルをドラッグした時に並び替えられるようにしたいです。
###発生している問題・エラーメッセージ
現状だと、tableviewの(3本の横棒)アイコンをドラッグしないと並びかえができません。
このアイコン部分だけではなく、セルをドラッグしたら並び替えできるようにしたいです。
###該当のソースコード
swift
1import UIKit 2 3class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { 4 5 @IBOutlet weak var tableView: UITableView! 6 var dataList = ["ゲスト1","ゲスト2","ゲスト3","ゲスト4"] 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 tableView.isEditing = true 11 tableView.isScrollEnabled = false 12 } 13 14 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 15 tableView.showsVerticalScrollIndicator = true 16 return dataList.count 17 } 18 19 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 20 let cell = tableView.dequeueReusableCell(withIdentifier: "SeatCell", for: indexPath) as UITableViewCell 21 22 cell.textLabel?.text = dataList[indexPath.row] 23 24 return cell 25 } 26 //移動許可 27 func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { 28 return true 29 } 30 //編集許可 31 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 32 return true 33 } 34 //追加・削除の禁止 35 func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { 36 return UITableViewCellEditingStyle.none 37 } 38 //セル移動時の配列データ処理 39 func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { 40 let targetTitle = dataList[sourceIndexPath.row] 41 if let index = dataList.index(of: targetTitle) { 42 dataList.remove(at: index) 43 dataList.insert(targetTitle, at: destinationIndexPath.row) 44 } 45 } 46 //削除アイコン表示分のスペースをインデントさせない 47 func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool { 48 return false 49 } 50}
回答1件
あなたの回答
tips
プレビュー