Last active
September 6, 2015 05:57
-
-
Save masakid/51e897bc570dfbef8c66 to your computer and use it in GitHub Desktop.
Lesson21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CheckItem.swift | |
import Foundation | |
import RealmSwift | |
class CheckItem : Object { | |
dynamic var id : Int16 = 0 | |
dynamic var name : String = "" | |
dynamic var isCheck : Bool = true | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CheckItemManager.swift | |
// KadaiCheck | |
import UIKit | |
import RealmSwift | |
class CheckItemManager: NSObject { | |
//Managerクラス | |
//役割 | |
//Realmからデータを取得の役割 | |
//返却する際はEntity, [Entity]で返す | |
//データ追加処理 | |
//引数で追加データを渡して、内部で登録処理実施 | |
func insert(id: Int16, name: String, isCheck: Bool){ | |
let realm = Realm() | |
let checkItem = CheckItem() | |
checkItem.id = id | |
checkItem.name = name | |
checkItem.isCheck = isCheck | |
realm.write { | |
realm.add(checkItem) | |
} | |
println("保存に成功!") | |
} | |
func insertAll(){ | |
} | |
//データ更新処理 | |
//1件に対して更新処理実施 | |
//idに一致するレコードのみ更新する | |
func updateWithName(id: Int16, name: String, isCheck: Bool){ | |
let realm = Realm() | |
let checkItems = realm.objects(CheckItem).filter("id = \(id)") | |
realm.write { | |
for checkItem in checkItems { | |
checkItem.name = name | |
checkItem.isCheck = isCheck | |
} | |
} | |
println("保存に成功!") | |
} | |
//データ検索処理 | |
//全件データを読み込んで表示 | |
//こちらも削除のNSPredicateを参考にして、検索処理は実装する | |
func selectAllData() -> [CheckItem]?{ | |
let realm = Realm() | |
let checkItems = realm.objects(CheckItem) | |
var checkItemArr: [CheckItem] = [] | |
for obj in checkItems { | |
println("id = \(obj.id), name = \(obj.name), isCheck = \(obj.isCheck)") | |
checkItemArr.append(obj) | |
} | |
return checkItemArr | |
} | |
//対象データを検索して返却 | |
func selectDataWithId(id: Int16) -> [CheckItem]?{ | |
let realm = Realm() | |
let checkItems = realm.objects(CheckItem).filter("id = \(id)") | |
var checkItemArr: [CheckItem] = [] | |
for checkItem in checkItems { | |
checkItemArr.append(checkItem) | |
} | |
return checkItemArr | |
} | |
//データ削除処理 | |
//検索して削除している。参考にするならここ! | |
func deleteWhereId(id : Int16){ | |
let realm = Realm() | |
let checkItems = realm.objects(CheckItem).filter("id = \(id)") | |
realm.write { | |
realm.delete(checkItems) | |
} | |
} | |
//全データ削除処理 | |
func deleteAll(){ | |
let realm = Realm() | |
realm.write { | |
realm.deleteAll() | |
} | |
} | |
func updateAllSeqId(){ | |
let realm = Realm() | |
let checkItems = realm.objects(CheckItem) | |
realm.write { | |
for (index, checkItem) in enumerate(checkItems) { | |
checkItem.id = Int16(index) | |
} | |
} | |
println("更新に成功!") | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CommonData.swift | |
// KadaiCheck | |
// | |
import UIKit | |
class CommonData: NSObject { | |
var fruitsArr: Array<CheckItem> = [] | |
func initData(){ | |
let manager = CheckItemManager() | |
if let arr = manager.selectAllData() { | |
self.fruitsArr = arr | |
} | |
} | |
func appendFruitsWithName(name:String, isCheck:Bool){ | |
//新規作成処理 | |
let manager = CheckItemManager() | |
//現在の登録レコード最大値 + 1 | |
let id = self.fruitsArr.count | |
manager.insert(Int16(id), name: name, isCheck: isCheck) | |
if let arr = manager.selectAllData() { | |
self.fruitsArr = arr | |
} | |
} | |
//共通データリストの更新 | |
func updateFruitsWithName(id: Int, name:String, isCheck:Bool){ | |
//idで検索して更新 | |
let manager = CheckItemManager() | |
manager.updateWithName(Int16(id), name: name, isCheck: isCheck) | |
if let arr = manager.selectAllData() { | |
self.fruitsArr = arr | |
} | |
} | |
//削除メソッド | |
func removeFruitsWithRow(indexRow: Int){ | |
//idで検索して削除 | |
let id = indexRow | |
let manager = CheckItemManager() | |
manager.deleteWhereId(Int16(id)) | |
//削除したのでid連番へ付け替え | |
manager.updateAllSeqId() | |
//保持しているデータをリフレッシュ | |
if let arr = manager.selectAllData() { | |
self.fruitsArr = arr | |
} | |
} | |
//番号を指定して取得 | |
//indexPath.rowを渡したら、対応する行が返却される | |
func displayRowNumberFruits(fruitsNum:Int) -> (id: Int16, name:String, isCheck:Bool)?{ | |
return (Int16(fruitsNum), fruitsArr[fruitsNum].name, fruitsArr[fruitsNum].isCheck) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment