2016-10-21 8 views
0

私の最初のコードUIViewControllerはこのように見え、基本的に私が入力したデータをUITextFieldに保存し、それを取り出してUITableViewに取り込みます。以下のコードを入力します。私は(コード怒鳴るなど)上記のVCに保存されたデータを取得する場所ですテーブルのコアデータに保存されたデータを読み込みます。

import UIKit 
    import CoreData 

    class ViewController: UIViewController { 
    @IBOutlet weak var textField1: UITextField! 

    override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    } 
    override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
    } 

    @IBAction func nxtbuttonpressed(_ sender: AnyObject) { 


    let appDelegate = UIApplication.shared.delegate as! AppDelegate 
    let context = appDelegate.persistentContainer.viewContext 
    //Lets save data 
    let newUser = NSEntityDescription.insertNewObject(forEntityName: "Expenses", into: context) 
    newUser.setValue(textField1.text, forKey: "expenseName") 

    do { 
     try context.save() 
     print("data saved successfully") 

    }catch{ 
     print("There was an error") 
    } 

    performSegue(withIdentifier: "ShowNxtVC", sender: nil) 

    } 
    } 

(私は私の問題はcellForRow方法であると思います)。ただし、すべてのデータをコンソールに印刷することはできますが、保存した最後のデータ項目のみがUITableViewCellに入力されます。どのようにしてこれを克服して、私が最後に保存したデータではなく、以前に保存したすべてのデータを読み込むことができます。

import UIKit 
    import CoreData 

class TableViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 
    @IBOutlet weak var tableView: UITableView! 
    var myArray : Array? = [] 

    override func viewDidLoad() { 
    super.viewDidLoad() 

    let appDelegate = UIApplication.shared.delegate as! AppDelegate 
    let context = appDelegate.persistentContainer.viewContext 

    let request = NSFetchRequest <NSFetchRequestResult> (entityName: "Expenses") 
    request.returnsObjectsAsFaults = false 

    do { 

     let results = try context.fetch(request) 


     // check data existance 
     if results.count>0 { 
      print(results.count) 

      for resultGot in results as! [NSManagedObject]{ 

       if let expName = resultGot.value(forKey:"expenseName") as? String{ 

        myArray = [expName] 


        print("my array is : \(myArray)") 
       } 
      } 

     } 

    }catch{ 


     print("No Data to load") 
    } 

    // Do any additional setup after loading the view. 
    } 

    override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
    } 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    print(myArray?.count) 
    return myArray!.count 

    } 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = UITableViewCell() 
    cell.textLabel?.text = myArray? [indexPath.row] as! String? 

    return cell 

    } 
} 
+0

if条件でmyArrayで配列を取得しましたか? –

答えて

0

オフあなたが持っているすべてこれを変更する:

myArray = [newValue] th atは、コアデータの新しい経費を見つけるたびに、値を1つだけ使用して配列をリセットします。

var array = [String]() 
array = ["hello"] 
array = ["joe"] 
print(array) 
// returns: ["joe"] 

使用:

myArray.append(newExpense) 

とあなたが得る:あなたが追加FORループの後

var array2 = [String]() 
    array2.append("hello") 
    array2.append("joe") 
    print(array2) 
    // returns: ["hello", "joe"] 

tableView.reloadData() 

を今、あなたはアップと

を実行する必要があります

あなたが行う必要があります別のものがあります:あなたはTableViewCellに再利用識別子としてExpCellを配置する必要があり、あなたのストーリーボードで

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "ExpCell", for: indexPath) 
    cell.textLabel?.text = myArray?[indexPath.row] as! String 
    return cell 
} 

が。 dequeReusableCellコマンドは、デバイス上に表示されているセルだけを読み込み、表示されていないセルをスクロールした後に再利用します。これにより、アプリのメモリ使用量が大幅に減り、処理速度が向上します。

0

データと更新テーブルビュー、最初にあなたの問題を解決するインデックスパス機能で行に対するmyArray = [expName]使用後

DispatchQueue.main.async { [unowned self] in 
     self.tableView.reloadData() 
    } 
+0

基本的に問題は私の配列に要素を追加し、tableviewをリロードする必要がありました。ありがとうございました。 – danutha

0

変更テーブルビューセルを

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
let cell = tableView.dequeueReusableCell(withIdentifier identifier: String, 
       for indexPath: IndexPath) 
if(!cell){ 
    cell = tableView.register(tableViewCellClass,forCellReuseIdentifier identifier: String) 
} 
return cell 
} 
関連する問題