当我打印var array = [NSManagedObject]()
时,我在调试窗口中得到[]
,并且我的表视图不会读取该数组。
我应该使用nsfetch还是什么从该数组中读取数据?我设置了CoreData,它可以正常工作,但当我使用prepare for segue table视图向另一个vc发送数据时,它不会读取var array = [NSManagedObject]()
。
第二个VC,也就是tableview:
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
}
}
发布于 2015-10-05 20:27:00
在我看来,问题不在于通过segue接收和发送数据。你的发送是正确的,这就是为什么你没有收到错误的原因。所以,问题出在您发送的数据中,当您发送数据时,NSManaged对象的数组是空的。我试着以同样的方式发送,它是工作的。不存在表视图不读取数组的问题。如果您的数组包含数据,则创建的单元格的数量将等于您的数组的元素数量,现在该数组为空。
发布于 2015-10-05 21:51:45
此表单
[NSManagedObject]()
是创建空数组的初始值设定项。您可以填充此数组并将其发送到下一个视图控制器,而不是发送将始终为空的新初始化的数组。
您应该使用NSFetchedResultsController
在表视图中显示核心数据项。有关如何使用该平台的许多问题,请参阅该平台。
https://stackoverflow.com/questions/32947400
复制相似问题