2015-10-05 149 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在表格视图中显示核心数据项目。有关描述如何使用它的许多问题,请参阅此平台。