2015-10-05 147 views
0

當我打印var array = [NSManagedObject]()時,我在調試窗口中得到了[],並且我的tableview不會讀取該數組。UITableView不讀取陣列

我應該使用nsfetch還是從數組中讀取數據?我設置了CoreData並且它可以工作,但是當我將數據發送到另一個vc並準備用於segue table view時,將不會讀取var array = [NSManagedObject]()

其次VC又名實現代碼如下:

import UIKit 
import CoreData 

class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

@IBOutlet weak var tableView: UITableView! 

var array = [NSManagedObject]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // self.array = self.bridge.item 
    print(array) 
    self.tableView.delegate = self 
    self.tableView.dataSource = self 


} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 

    let person = array[indexPath.row] 
    cell.textLabel!.text = String(person) 

    return cell 
} 

}

+0

你在哪裏將數據加載到array中?在第一個vc中用var item = [NSManagedObject]()有 – Miknash

+0

,有數據,並且我發送它的那個對象項準備繼承var array = [NSManagedObject]()在第二個vc中,也就是tableview – Igy

+0

這裏是完整的項目http:/ /stackoverflow.com/questions/32863651/swift-2-0-core-data-tableview-not-reading-nsmanagedobject?noredirect=1#comment53566221_32863651 – Igy

回答

1

從我所看到的,這個問題是不是在接收和通過SEGUE發送數據。你正確地發送這就是爲什麼你沒有得到錯誤。所以,問題出在您發送的數據上,當您發送時,NSManaged對象的數組是空的。我嘗試以同樣的方式發送,它正在工作。如果數組包含數據,那麼表格視圖不會讀取數組,如果您的數組包含數據,則會創建與您的數組元素數量相等的單元格數量,該數量現在爲空。

+0

這裏是我的完整項目,在第一個VC如何存儲數據,然後在一些屬性對象然後? http://stackoverflow.com/questions/32863651/swift-2-0-core-data-tableview-not-reading-nsmanagedobject?noredirect=1#comment53566221_32863651 – Igy

+0

你發送的數組結果是哪一個? –

1

這種形式

[NSManagedObject]() 

是創建一個空數組的初始化。你可能會填充此數組,並將其發送到您的下一個視圖控制器,而不是發送新的初始化一個將永遠是空的。

應該使用NSFetchedResultsController在表格視圖中顯示核心數據項目。有關描述如何使用它的許多問題,請參閱此平臺。