ios - Hooking UITableView to a JSON API -
i'm quite new swift i'm trying hook uitableview best possible way. i've decided use swiftyjson, seem simple. objects in json object following:
{ "id": "146", "title": "esports site streak provides prize-heavy alternative traditional betting", "url": "http://www.dailydot.com/esports/streak-counter-strike-vulcun-betting/", "image_url": "//cdn0.dailydot.com/cache/bb/cc/bbccc49d8271f2f3ed4c40b45c0fe0c0.jpg", "date": "2015-04-10 22:07:00", "news_text": "test teeeext", "referer_img": "1" }
so far i've started creating loop creates loop through loops in viewdidload
for (key: string, subjson: json) in jsonarray { println(subjson) }
and after i've created class news below:
class news { var id: int! var title: nsstring! var link: nsstring! var imagelink: nsstring! var summary: nsstring! var date:nsstring! init(id: int, title:nsstring, link: nsstring, imagelink:nsstring, summary: nsstring, date:nsstring) { self.id = id self.title = title self.link = link self.imagelink = imagelink self.summary = summary self.date = date } }
however i'm not sure whether best approach creating this? next steps hook uitableview?
this should give idea of do. you'll need create array first. you'll creating , adding news object 1 one array. you'll use array tableview's datasource. in cellforrowatindexpath you'll read in news objects , display data.
var arraynews = array<news>() self.tableview.datasource = self (key: string, subjson: json) in jsonarray { // create object , parse json 1 one append array var newnewsobject = news() id: = // title: = // link = // imagelink = // summary = // date = // arraynews.append(newnewsobject) } self.tableview.reloaddata() // read in arraynews array func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell = tableview.dequeuereusablecellwithidentifier("newscell") as! newscell let newsobject = self.arraynews[indexpath.row] // assuming 1 section // set of details here cell.labelblahblah.text = return cell }
Comments
Post a Comment