Reputation: 129
When I print var array = [NSManagedObject]()
I get []
in debugging window, and my tableview would not read that array.
Should I use nsfetch or what to read data from that array? I set up CoreData and it works, but when I send data to another vc with prepare for segue table view won't read var array = [NSManagedObject]()
.
Second VC aka 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
}
}
Upvotes: 0
Views: 73
Reputation: 80271
This form
[NSManagedObject]()
is an initializer that creates an empty array. You could populate this array and send it to your next view controller rather than sending newly initialized one which will always be empty.
You should use a NSFetchedResultsController
to display Core Data items in a table view. See this platform for many questions that describe how to use it.
Upvotes: 1
Reputation: 316
From what I can see, the problem is not in receiving and sending data through segue. You are sending it rightly that's why you are not getting errors. So, the problem is in the data you are sending, that array of the NSManaged object is empty when you are sending. I tried sending in the same way, it is working. There is no question of table view not reading array if your array contains data then number of cells will be created equal to the number of elements of your array, which right now is empty.
Upvotes: 1