当我打印 var array = [NSManagedObject]()
时,我在调试窗口中得到 []
,而我的 tableview 不会读取该数组。
我应该使用 nsfetch 还是什么来从那个数组中读取数据?我设置了 CoreData 并且它可以工作,但是当我将数据发送到另一个带有 prepare for segue table view 的 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
}
最佳答案
据我所知,问题不在于通过 segue 接收和发送数据。您发送正确,这就是您没有收到错误的原因。因此,问题出在您发送的数据中,发送时 NSManaged 对象的数组为空。我尝试以同样的方式发送,它正在工作。毫无疑问,如果您的数组包含数据,那么 TableView 不会读取数组,那么将创建的单元格数量等于您的数组元素的数量,现在它是空的。
关于ios - UITableView 不读取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32947400/