Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

José Manuel Martínez López
José Manuel Martínez López
18,133 Points

How can i display information in a table view?

I found on internet how to display it but it´s not showing anything. Here´s my code:

import UIKit import CoreData

class Ingresos: UIViewController{ @IBOutlet var des:UITextField! @IBOutlet var cost:UITextField! @IBOutlet var tbl:UITableView! var myData: Array<AnyObject>=[]

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

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillAppear(animated: Bool) {
    tbl.reloadData()
}


override func viewDidAppear(animated: Bool){

    let appDel: AppDelegate=UIApplication.sharedApplication().delegate as AppDelegate
    let context: NSManagedObjectContext=appDel.managedObjectContext!


    var request=NSFetchRequest(entityName: "Ingresos")
    request.returnsObjectsAsFaults=false

    var error : NSError?
    myData=context.executeFetchRequest(request, error: &error)!

    tbl.reloadData()
}

func numberOfSectionsInTableView(tableView: UITableView?)->Int{
    return 1
}

 func tableView(tableView: UITableView, numberOfRowsInSection section:Int)->Int{
    return myData.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell?{

    let cellID:NSString="cell"

    var cell:UITableViewCell=tableView.dequeueReusableCellWithIdentifier(cellID) as UITableViewCell

    if let ip=indexPath{
        var data:NSManagedObject=myData[ip.row] as NSManagedObject
        cell.textLabel?.text=data.valueForKeyPath("descripcion") as? String
        cell.detailTextLabel?.text=data.valueForKeyPath("dinero") as? String
    }

    return cell
}

func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath:NSIndexPath){
    if(editingStyle==UITableViewCellEditingStyle.Delete){
    }
}

@IBAction func accept(sender:UIButton){

    let appDel: AppDelegate=UIApplication.sharedApplication().delegate as AppDelegate
    let context: NSManagedObjectContext=appDel.managedObjectContext!
    var newItem=NSEntityDescription.insertNewObjectForEntityForName("Ingresos", inManagedObjectContext: context) as NSManagedObject
    newItem.setValue(des.text, forKey: "descripcion")
    newItem.setValue(cost.text, forKey: "dinero")
    var error : NSError?
    context.save(&error)
    println(newItem)
    println("saved")


}

@IBAction func load(sender: UIButton){

    let appDel: AppDelegate=UIApplication.sharedApplication().delegate as AppDelegate
    let context: NSManagedObjectContext=appDel.managedObjectContext!


    var request=NSFetchRequest(entityName: "Ingresos")
    request.returnsObjectsAsFaults=false

    var error : NSError?
    var results:NSArray=context.executeFetchRequest(request, error: &error)!

    if(results.count>0){
        for res in results{
            println(res)
            println("Loaded!")
        }

    }

    else{
        let alertController=UIAlertController(title: "Error", message: "No ha almacenado nada", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alertController, animated:true, completion:nil)

    }

}

}