LoginSignup
0
1

More than 1 year has passed since last update.

TableViewCellを並び替える機能を使ってみる

Posted at

今回の内容

215AE899-50C0-4EAE-9A90-31EA035E6CA5_1_201_a.jpeg A16C72EA-38C8-469C-A202-B5DD135CF614_1_201_a.jpeg

コードと簡単解説

  • func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {}でcellを並び替える事を可能にするかをtrueまたはfalseで設定します。

  • func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {}で、cellを並び替えるときに働かせる処理を設定します。

  • func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {}でcellの左側に表示する内容を設定します。今回は、.noneで何も表示しないようにしています。

  • editingStyleForRowAt.noneで設定した事によって残ってしまった空白を削除する為に、func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {}falseで設定します。

import UIKit

class ViewController: UIViewController {

    let tableView = UITableView()

    var cellContentsArray = ["UIKit","SwiftUI","MapKit","ARKit","CoreML","WebKit","Foundation","LocalAuthentication","CoreGraphics","Combine"]

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        tableView.frame = CGRect(x: view.frame.minX, y: view.frame.minY, width: view.frame.width, height: view.frame.height)
        tableView.isEditing = true
        view.addSubview(tableView)

        tableView.dataSource = self
        tableView.delegate = self
    }
}

extension ViewController:UITableViewDataSource{

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return cellContentsArray.count
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = cellContentsArray[indexPath.row]

        return cell
    }

    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {

        return true
    }

    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {

        let cellContent = cellContentsArray[sourceIndexPath.row]
        cellContentsArray.remove(at: sourceIndexPath.row)
        cellContentsArray.insert(cellContent, at: destinationIndexPath.row)
    }

}

extension ViewController:UITableViewDelegate{

    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {

        return .none
    }

    func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {

        return false
    }
}

終わり

ご指摘、ご質問などありましたら、コメントまでお願い致します。

0
1
0

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
0
1