2016-12-15 11 views
0

レルムで2つのテキストファイルを保存して削除するにはどうすればよいですか?レルムで2つのテキストファイルを保存して削除するにはどうすればよいですか?

class ViewController: UIViewController , UITableViewDataSource , UITableViewDelegate { 
    @IBOutlet weak var text1: UITextField! 
    @IBOutlet weak var text2: UITextField! 
    @IBOutlet weak var ttableview: UITableView! 

    //delete row and tableview and array 
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
     if editingStyle == .delete { 
      array1.remove(at: indexPath.row) 
      array2.remove(at: indexPath.row) 
      ttableview.deleteRows(at: [indexPath], with: .fade) 
     } 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return array1.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = ttableview.dequeueReusableCell(withIdentifier: "cell") as! Cell 
     cell.lable1.text = array1[indexPath.row] 
     cell.lable2.text = array2[indexPath.row] 
     return cell 
    } 

    var array1 = [String]() 
    var array2 = [String]() 
} 

私は保存し、テーブルビューに私の2つのテキストフィールドを削除する:続き

は私のコードです。以下は

enter image description here

あなたが最初のレルムのマニュアルを読む必要があり

@IBAction func Add(_ sender: Any) { 
    array1.insert(text1.text!, at: 0) 
    array2.insert(text2.text!, at: 0) 
    self.ttableview.reloadData() 
} 

func queryPeople(){ 
    let realm = try! Realm() 
    let allPeople = realm.objects(CCat.self) 
    for person in allPeople { 
     print("\(person.name) to \(person.job)") 
    } 
} 
+0

このコードで直面している問題は何か言及してください、また、いくつかの説明を追加してください。 –

+0

ここで領域を使用する方法私は削除のデータを保存したい私はあなたが私を助けることができるコードをしたいですか? –

答えて

0

をテーブルビューに追加するためのコードです。 https://realm.io/docs/swift/latest/

オブジェクトをRealmに保存するには、モデルクラスをObjectというサブクラスとして定義します。

class Person: Object { 
    // Optional string property, defaulting to nil 
    dynamic var name: String? = nil 

    // Optional int property, defaulting to nil 
    // RealmOptional properties should always be declared with `let`, 
    // as assigning to them directly will not work as desired 
    let age = RealmOptional<Int>() 
} 

次に、モデルクラスのインスタンスを作成します。

let author = Person() 
author.name = "David Foster Wallace" 

Realmのインスタンスを作成します。

// Get the default Realm 
let realm = try! Realm() 
// You only need to do this once (per thread) 

トランザクションを開始し、レルムにオブジェクトを追加します。

// Add to the Realm inside a transaction 
try! realm.write { 
    realm.add(author) 
} 
関連する問題