我想填充一个UITableViewController
,所以在viewDidLoad
我的代码中填写:
override func viewDidLoad() {
let rc = RestCaller()
rc.get( "events" , authenticate: true) {
error, data in
if error != nil {
print("error")
}
let events = JSON( data: data )
self.events = events["data"].arrayValue
}
}
并且有我的get函数:
func get( route: String, authenticate: Bool?, next: (error: NSError?, data: NSData) -> Void )
{
let request = NSMutableURLRequest( URL: NSURL( string: self.baseUrl + route )! )
request.HTTPMethod = "GET"
if authenticate != nil {
request.addValue( getUserProperty( "USER_TOKEN" ) as! String, forHTTPHeaderField: "usertoken")
request.addValue( (getUserProperty( "USER_ID" )?.stringValue)!, forHTTPHeaderField: "userid" )
}
_ = NSURLSession.sharedSession().dataTaskWithRequest( request ) {
data, response, error in
next(error: error, data: data!)
}.resume()
}
但是在numberOfRowsInSection
方法中:
return events.count // return 0
但如果我在这里打印self.events:
override func viewDidLoad() {
...
let events = JSON( data: data )
self.events = events["data"].arrayValue
print(self.events) // not empty
...
}
我不明白为什么:(
有什么想法吗?谢谢!
答案 0 :(得分:0)
好像你忘了重装表。
override func viewDidLoad() {
let rc = RestCaller()
rc.get( "events" , authenticate: true) {
error, data in
if error != nil {
print("error")
}
let events = JSON( data: data )
self.events = events["data"].arrayValue
dispatch_async(dispatch_get_main_queue()) { () -> Void in
self.tableView.reloadData()
}
}