ソースはこちら
Realmを使用してTodoリストを作成していきます。
過去記事はこちら
COreDataを使用したTodoリストはこちら。
→ https://qiita.com/sventouz/items/b3265be8d2c3f8e44f71
テーブルビューを利用したToDoリストはこちら。
→ https://qiita.com/sventouz/items/d710c1f0c48c0da90a69
Realmのインストール
RealmはDBのようなものです。
読み方は「レルム」と読みます。
pod version
でエラーが出なければ
pod init
でPodfileが自動で生成されます。
そのPodfileの
use_frameworks!
の下に
pod 'RealmSwift'
を追記してください。
これで
pod install
でrealmを使用することができます。
全体のコードはこちら
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'MyRealmTodo' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
pod 'RealmSwift'
# Pods for MyRealmTodo
target 'MyRealmTodoTests' do
inherit! :search_paths
# Pods for testing
end
target 'MyRealmTodoUITests' do
# Pods for testing
end
end
画面作成
全体像はこんな感じ。
クラスの作成
Realmを使用するにはクラスの作成が必要です。
command+N でファイルの新規作成して、swiftファイルを選択。
名前は「Todo.swift」にしました。
import Foundation
import RealmSwift
class Todo:Object {
@objc dynamic var title = ""
}
ViewControllerの中身
まずnavigation controllerを追加する。
右上にはBar Button Itemを使用する。
以下の設定でアイコンを+できる。
これで見た目は完成。
table置いてcellを置いて、cellのidentifierはcellで設定。
以下がコード
import UIKit
import RealmSwift
class ViewController: UIViewController, UITableViewDataSource {
var todoItems: Results<Todo>!
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
table.dataSource = self
let realm = try! Realm()
todoItems = realm.objects(Todo.self)
table.reloadData()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
table.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return todoItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let object = todoItems[indexPath.row]
cell.textLabel?.text = object.title
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
deleteTodo(at:indexPath.row)
table.reloadData()
}
}
func deleteTodo(at index: Int) {
let realm = try! Realm()
try! realm.write {
realm.delete(todoItems[index])
}
}
}
addViewControllerの中身
addViewControllerは初めからはないので、作成します。
command+Nでファイル作成。
cocoaPodsファイルで新規作成。名前は「addViewController.swift」
作成したaddViewControllerとstoryboardを関連付けさせなければいけません。
こんな感じ。
storyboard内は以下の通り。
Labelで文字をつけます。
textFieldで文字を入力する場所を設置します。
追加ボタンを置くだけです。
ソースはこちら
import UIKit
import RealmSwift
class addViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var textBox: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textBox.delegate = self
}
@IBAction func addbtn(_ sender: Any) {
let realm = try! Realm()
let todo = Todo()
todo.title = textBox.text!
try! realm.write {
realm.add(todo)
}
self.navigationController?.popViewController(animated: true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.textBox.resignFirstResponder()
return true
}
}
関連記事
COreDataを使用したTodoリストはこちら。
→ https://qiita.com/sventouz/items/b3265be8d2c3f8e44f71
テーブルビューを利用したToDoリストはこちら。
→ https://qiita.com/sventouz/items/d710c1f0c48c0da90a69
参考
@Shiro_Head 参考にさせていただきました。ありがとうございました。