Ios UITableView未读取数组
当我打印Ios UITableView未读取数组,ios,swift,uitableview,core-data,Ios,Swift,Uitableview,Core Data,当我打印var array=[NSManagedObject]()时,我在调试窗口中得到[],而我的tableview不会读取该数组 我应该使用nsfetch还是从该数组读取数据?我设置了CoreData,它可以工作,但是当我用prepare for segue表视图将数据发送到另一个vc时,它不会读取var数组=[NSManagedObject]() 第二个表视图: import UIKit import CoreData class TableViewController: UIViewC
var array=[NSManagedObject]()
时,我在调试窗口中得到[]
,而我的tableview不会读取该数组
我应该使用nsfetch还是从该数组读取数据?我设置了CoreData,它可以工作,但是当我用prepare for segue表视图将数据发送到另一个vc时,它不会读取var数组=[NSManagedObject]()
第二个表视图:
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对象的数组在发送时为空。我试着用同样的方式发送,它正在工作。不存在表视图不读取数组的问题。如果数组包含数据,则创建的单元格数将等于数组的元素数,此时数组为空 这张表格
[NSManagedObject]()
是创建空数组的初始值设定项。您可以填充此数组并将其发送到下一个视图控制器,而不是发送新初始化的始终为空的视图控制器
您应该使用
NSFetchedResultsController
在表视图中显示核心数据项。有关如何使用它的许多问题,请参阅此平台。您将数据加载到数组中的位置?在第一个vc中,var item=[NSManagedObject](),有数据,我将其发送到第二个vc中的var arraY=[NSManagedObject]()这是完整的项目这是我的完整项目,在vc中如何先存储数据,然后在某个属性对象中存储数据呢?将结果发送到哪个阵列?