gpt4 book ai didi

ios - 如何让 NSURLConnection.sendAsynchronousRequest 在 UITableView 初始化之前执行

转载 作者:行者123 更新时间:2023-11-28 12:59:12 25 4
gpt4 key购买 nike

我已经声明了一个数组并通过从互联网获取数据来初始化它。我希望它在 UITableView 初始化之前初始化,因为它是为了填充 UITableView 的单元格,但显然它没有。我可以做些什么来改进代码终端打印1个2个2个2个3个4个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)
}
}


}

最佳答案

您不需要在初始化 TableView 之前初始化数组,当您的数据数组准备就绪时 - 只需调用

[tableView reloadData];

关于ios - 如何让 NSURLConnection.sendAsynchronousRequest 在 UITableView 初始化之前执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34283822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com