ios - Why is the text label empty when debugger says it is not? Swift app? -
i'm new swift , i'm trying text label show on table view isn't showing up. following file , build.
tree nest of ui: --main view controller ----ui tabel view ------main view controller cell
i'm beginning think i'm reloading table @ wrong place.
i appreciate in advance!
when remove self.maintableview.reloaddata(), no cells show up. @ moment, i'm still able click on cells , respond following debugger lines:
selected number 0 selected number 1 selected number 2
mainviewcontroller.swift
import foundation import locksmith import uikit import alamofire import swiftyjson import alamofire import jwtdecode class mainviewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource { var userfeed = [userfeedrow]() @iboutlet weak var maintableview: uitableview! override func viewdidload() { maintableview.registerclass(mainviewtableviewcell.self, forcellreuseidentifier: "maintableviewcell") maintableview.delegate = self maintableview.datasource = self maintableview.separatorstyle = uitableviewcellseparatorstyle.none } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return self.userfeed.count } func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { println("you selected number \(indexpath.row)") } func numberofsectionsintableview(tableview: uitableview) -> int { return 1 } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { println("celler \(indexpath.row)") var cell:mainviewtableviewcell = self.maintableview.dequeuereusablecellwithidentifier("maintableviewcell", forindexpath:indexpath) mainviewtableviewcell let feedrow = userfeed[indexpath.row] println(feedrow.title) cell.maintableviewcelltitle?.text = feedrow.title return cell } override func viewdidappear(animated: bool) { // alamofire user/user_feed // display user feed in tableview inside of uiviewcontroller loaduserfeed() } func loaduserfeed(){ println("loaduserfeed") let api = api() api.getuserfeed(){ responseobject, error in var json = json(responseobject!) if error == nil{ // feed var responsefeed = json["feed"] //println(json) (index: string, subjson: json) in responsefeed { var atitle = subjson["title"].stringvalue println(atitle) // save object array here var row = userfeedrow(title: atitle) self.userfeed.append(row) // appending userfeed } self.maintableview.reloaddata() } } } }
custom tableview cell
import foundation import uikit class mainviewtableviewcell: uitableviewcell{ @iboutlet weak var maintableviewcellbackgroundimage: uiimageview! @iboutlet weak var maintableviewcelltitle: uilabel! override func awakefromnib() { super.awakefromnib() } override func setselected(selected: bool, animated: bool) { super.setselected(selected, animated: animated) } }
i'll convert comment answer , append explanation.
there 2 ways of dealing custom uitableviewcell
s: 1 create them through storyboard (prototype cells), you've done, , (usually) assign them custom class; other create them separately (with custom class, or custom nib file, or custom class + nib file).
creating cells in storyboard registerclass:forcellreuseidentifier:
dance in background. when call method in code, severs connection between storyboard cell , custom class made, , instead creates cell custom class, in case contains no view information, , renders empty.
an additional word of advice: if think might need particular cell in more place 1 in app, better create nib + class method, , call registernib:forcellreuseidentifier:
want use it. this, instead of copy-pasting cell in whichever storyboard view need (and have change of them individually if change needed), have single view/nib maintain throughout whole app.
Comments
Post a Comment