magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.8k stars 1.79k forks source link

Please help me TableView and CoreData #1239

Closed KHALIDALHARTHI closed 5 years ago

KHALIDALHARTHI commented 8 years ago

There is no data in the TableView and No error message Please help me thanks

` import UIKit import CoreData

class ViewController2: UIViewController ,UITableViewDelegate,UITableViewDataSource,NSFetchedResultsControllerDelegate{ let cllid = "MYCell"

@IBOutlet weak var mytabl: UITableView!
  var ccont=(UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var err:NSError?
var frc:NSFetchedResultsController=NSFetchedResultsController()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

    self.mytabl.registerClass(UITableViewCell.self, forCellReuseIdentifier: cllid)
    self.mytabl.delegate=self
    self.mytabl.dataSource=self

     lodData()

    // Dispose of any resources that can be recreated.

}
func lodData()
{
    let req=NSFetchRequest(entityName: "Khalid")

    self.frc=NSFetchedResultsController(fetchRequest: req, managedObjectContext: ccont, sectionNameKeyPath: nil, cacheName: nil)
    self.frc.delegate=self
    do {
        try self.frc.performFetch()
        print("fetch ok")

    }catch

    {
        print("fetch error")
    }

}
///////
//
////////
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    self.mytabl.reloadData()
    /////

    ///////////////
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    let info=self.frc.sections![section] as NSFetchedResultsSectionInfo
    return info.numberOfObjects

}

// ///////////////////////////////////////////////////// // // // ///////////////////////// func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = mytabl.dequeueReusableCellWithIdentifier(cllid, forIndexPath: indexPath) as UITableViewCell let khalid:AnyObject=self.frc.objectAtIndexPath(indexPath)

    cell.textLabel?.text=(khalid.valueForKey("nema") as! String)

    return cell
}

 func numberOfSectionsInTableView(tableView: UITableView) -> Int // Default is 1 if not implemented
 {

return 1
}

}`

hardikdevios commented 8 years ago

@KHALIDALHARTHI Please post on Stackoverflow.

KHALIDALHARTHI commented 8 years ago

What do you mean Stackoverflow

vshahpledge commented 8 years ago

www.stackoverflow.com

danielgalasko commented 8 years ago

The error is that you are setting your tables datasource in didReceiveMemoryWarning. Probably the last place it should be. This issue can be closed