0

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
}

}

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Igy
  • 129
  • 1
  • 16
  • where do you load your data into the arraY? – Miknash Oct 05 '15 at 11:28
  • in first vc with var item = [NSManagedObject](), there is data, and that object item i send it with prepare to segue to var array = [NSManagedObject]() in second vc aka tableview – Igy Oct 05 '15 at 11:31
  • here's complete project http://stackoverflow.com/questions/32863651/swift-2-0-core-data-tableview-not-reading-nsmanagedobject?noredirect=1#comment53566221_32863651 – Igy Oct 05 '15 at 11:33

2 Answers2

1

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.

Mukesh Thawani
  • 316
  • 5
  • 12
  • here's my full project, in first vc how to store data then in some property object then? http://stackoverflow.com/questions/32863651/swift-2-0-core-data-tableview-not-reading-nsmanagedobject?noredirect=1#comment53566221_32863651 – Igy Oct 05 '15 at 12:31
  • which array you are sending results one? – Mukesh Thawani Oct 05 '15 at 12:37
1

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.

Mundi
  • 79,884
  • 17
  • 117
  • 140