LoginSignup
3
5

More than 5 years have passed since last update.

Swift3 - UITableView並び替え

Posted at

参考

myItemsはArray

実装




    // (5) 移動を有効
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    // (6) 移動時の処理
    func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {

        // データの順番を整える
        let targetTitle = myItems[sourceIndexPath.row]
        if let index = myItems.index(of: targetTitle) {
            myItems.remove(at: index)
            myItems.insert(targetTitle, at: destinationIndexPath.row)
            print(myItems)
        }
    }

    private func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath as IndexPath, animated: true)
        print(myItems,

            [indexPath.row])
    }
3
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5