LoginSignup
0
1

More than 1 year has passed since last update.

TableView(xibファイル利用)で簡単なToDoアプリ風を作成

Last updated at Posted at 2021-06-05

簡単なToDoアプリ風を作成してみた。

xibファイル利用して、Cellの再利用を実装。

完成形はこんな感じ↓

アラート-2021-06-05-17.23.47.gif

FirstViewController
import UIKit

final class FirstViewController: UIViewController {

    private let cellClassName = "TableViewCell"
    private let reuseId = "TableViewCell"

    private var texts:[Model] = []
    private var heightCache: [CGFloat] = []


    @IBOutlet weak var tableView: UITableView!{
        didSet{
            let cellNib = UINib(nibName: cellClassName, bundle: nil)
            tableView.register(cellNib, forCellReuseIdentifier: reuseId)
            tableView.delegate = self
            tableView.dataSource = self
        }
    }

    @IBAction func addButtun(_ sender: Any) {

        var textField = UITextField()

                let alert = UIAlertController(title: "新しいアイテムを追加", message: "", preferredStyle: .alert)

                let action = UIAlertAction(title: "リストに追加", style: .default) { (action) in
                    // 「リストに追加」を押された時に実行される処理

                    let newItem: Model = Model(text: textField.text!)

                    // アイテム追加処理
                    self.texts.append(newItem)
                    self.tableView.reloadData()

                }

                alert.addTextField { (alertTextField) in
                    alertTextField.placeholder = "新しいアイテム"
                    textField = alertTextField
                }

                alert.addAction(action)
                present(alert, animated: true, completion: nil)

    }

        override func viewDidLoad() {
        super.viewDidLoad()

    }


}


extension FirstViewController:UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return texts.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: reuseId, for: indexPath) as? TableViewCell else {
              return UITableViewCell()
            }
        let user = texts[indexPath.row]
        cell.textLabel?.text = user.text
            cell.configure(user: user)
            return cell
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        texts.remove(at: indexPath.row)
        let indexPaths = [indexPath]
        tableView.deleteRows(at: indexPaths, with: .automatic)
    }

}

extension FirstViewController:UITableViewDelegate {


}
TableViewCell

import UIKit

class TableViewCell: UITableViewCell {

    @IBOutlet private weak var Label: UILabel!

    override func prepareForReuse() {
        super.prepareForReuse()
        Label?.text = nil
    }

    func configure(user: Model) {
        Label?.text = user.text
    }

    static func cellHeight(user: Model) -> CGFloat {
        /*読み込むデータに対して高さだけ決める*/
        return 1000
      }
}
Model

import Foundation

struct Model{
    var text:String = ""
}
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