Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJosé Manuel Martínez López
18,133 PointsHow 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)
}
}
}