我已经声明了一个数组,并通过从互联网获取数据来初始化它。我希望在UITableView初始化之前初始化它,因为它是要填充UITableView的单元格,但显然它没有。我可以做什么来改进代码终端打印1 2 2 2 3 4我希望它打印1 4 4 2 2 2 3
HttpController.swift
import UIKit
protocol HttpProtocol{
func didRecieveResults(results:NSDictionary)
}
class HttpController:NSObject{
var delegate:HttpProtocol?
func onSearch(url:String){
var nsUrl:NSURL = NSURL(string:url)!
var request:NSURLRequest = NSURLRequest(URL: nsUrl)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {
(response:NSURLResponse!,data:NSData!,error:NSError!)->Void in
var jsonResult:NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary
self.delegate?.didRecieveResults(jsonResult)
})
}
}ViewController.swift
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,HttpProtocol {
@IBOutlet weak var tv: UITableView!
@IBOutlet weak var iv: UIImageView!
@IBOutlet weak var playTime: UILabel!
@IBOutlet weak var progressView: UIProgressView!
var eHttp:HttpController = HttpController()
var tableData:NSArray = NSArray()
var channelData:NSArray = NSArray()
override func viewDidLoad() {
super.viewDidLoad()
eHttp.delegate = self
eHttp.onSearch("http://www.douban.com/j/app/radio/channels")
eHttp.onSearch("http://douban.fm/j/mine/playlist?channel=0")
println(1)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
println(2)
return 1//tableData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell!{
println(3)
let cell = UITableViewCell(style:UITableViewCellStyle.Subtitle,reuseIdentifier:"douban")
// let rowData:NSDictionary = self.tableData[indexPath.row] as! NSDictionary
cell.textLabel!.text = "hehehehe"//rowData["title"] as! String
cell.detailTextLabel!.text = "adasdasda"//rowData["artist"] as! String
return cell
}
func didRecieveResults(results:NSDictionary){
println(4)
if (results["song"] != nil){
self.tableData = results["song"] as! NSArray
}else if (results["channels"] != nil){
self.channelData = results["channels"] as! NSArray
// println(channelData)
}
}
}发布于 2015-12-15 16:17:46
当您的数据数组准备就绪时,您不需要在初始化表视图之前初始化数组-只需调用
[tableView reloadData];https://stackoverflow.com/questions/34283822
复制相似问题