ios - JSON Array and CoreData in populating TableView -
sorry inconvenience, ios-beginner , have problem...
i have json, coredata , tableview, json fetched web , saving in documents-folder. after that, values json saves in coredata , tableview must populated it. f.e. take event_title..
i have controller:
import foundation import uikit import corelocation import coredata class questlistviewcontroller: uiviewcontroller, uitableviewdatasource,uitableviewdelegate { let managedobjectcontext = (uiapplication.sharedapplication().delegate as! appdelegate).managedobjectcontext let eventscd = [events]() @iboutlet weak var queststabbaritem: uitabbaritem! var questcode : string = "" var numberofcells:int = 0 @iboutlet weak var tableview: uitableview! var numbers:[string] = [] override func viewdidload() { super.viewdidload() let entity = nsentitydescription.entityforname("events", inmanagedobjectcontext: managedobjectcontext!) let events = events(entity: entity!, insertintomanagedobjectcontext: managedobjectcontext) var error: nserror? var jsonerror:nserror? let jsonbundle = nsbundle.mainbundle() let jsonfolder = nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true)[0]as! string let jsonpath = jsonfolder.stringbyappendingpathcomponent("\(self.questcode).json") if let data = nsdata(contentsoffile:jsonpath, options:nsdatareadingoptions.datareadinguncached, error:&jsonerror) { let stringdata = nsstring(data: data, encoding: nsutf8stringencoding) let jsondict : nsdictionary? = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: &jsonerror) as? nsdictionary if let err = jsonerror { println("error parsing json") } if let jsondictionary = jsondict { println("json parsed successfully") let json_data = json(jsondictionary).arrayvalue let id = jsondictionary["id"]!.stringvalue let version: anyobject = jsondictionary["version"]! let title: anyobject = jsondictionary["title"]! let description: anyobject = jsondictionary["description"]! let events_json = jsondictionary["events"] as! nsarray let eventnumber: nsnumber = jsondictionary["events"]!.count nsnumber self.numberofcells = events_json.count } } // additional setup after loading view, typically nib. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } // mark: - table view data source func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { tableview.deselectrowatindexpath(indexpath, animated: true) let row = indexpath.row } // mark: - set number of rows func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return self.numberofcells } // mark: - create cell func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! uitableviewcell cell.backgroundcolor = uicolor.clearcolor() cell.backgroundview = uiview.new() cell.selectedbackgroundview = uiview.new() var imageview = uiimageview(frame: cgrectmake(10, 10, cell.frame.width - 0.3, cell.frame.height - 0.3)) let image = uiimage(named: "event_list_cell.png") imageview.image = image cell.backgroundview = uiview() cell.backgroundview!.addsubview(imageview) var row = indexpath.row let eventsc = eventscd[indexpath.row] /////here fatal breakpoint // cell.textlabel?.text = eventtable[row] as? string cell.textlabel?.text = eventsc.valueforkey("event_title")![row] as? string return cell } } a class events: import foundation import coredata
@objc(events) class events: nsmanagedobject { @nsmanaged var title: string @nsmanaged var event_title: string } and coremap: http://i.imgur.com/5ikwayn.png
and have error:
on line "let eventsc = eventscd[indexpath.row]" in controller output: "fatal error: array index out of range" where wrong?
this because in viewdidload setting numberofcells value > 0. don't have in eventscd. why when trying read value eventscd, index out of range error.
ideally number of rows returned should eventscd.count , eventscd should populated before loading table contents (or table reloaded once data , add values eventscd)
Comments
Post a Comment