首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在UITableView初始化之前让NSURLConnection.sendAsynchronousRequest执行

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

Stack Overflow用户
提问于 2015-12-15 16:02:42
回答 1查看 77关注 0票数 0

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

HttpController.swift

代码语言:javascript
复制
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

代码语言:javascript
复制
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)
        }
    }


}
EN

回答 1

Stack Overflow用户

发布于 2015-12-15 16:17:46

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

代码语言:javascript
复制
[tableView reloadData];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34283822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档