ViewTable 不读取数组(IOS)

Posted

技术标签:

【中文标题】ViewTable 不读取数组(IOS)【英文标题】:ViewTable don't read array (IOS) 【发布时间】:2016-04-14 12:05:00 【问题描述】:

我有 2 个程序,第一个是在线读取 JSON 并将其保存到数组中。第二个程序用数组完成UITableView,但是当我加入时不起作用。

当我调试时我先看表做,但我先有子方法。

class ViewController: UIViewController, UITableViewDataSource,    UITableViewDelegate 

@IBOutlet weak var tableView: UITableView!

var TableData:Array< String > = Array < String > ()

let textCellIdentifier = "TextCell"


override func viewDidLoad() 
    super.viewDidLoad()


    //--------------------------------------------------------------------------------------------------

    let requestURL: NSURL = NSURL(string: "http://www.learnswiftonline.com/Samples/subway.json")!
    let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithRequest(urlRequest) 
        (data, response, error) -> Void in

        let httpResponse = response as! NSHTTPURLResponse
        let statusCode = httpResponse.statusCode

        if (statusCode == 200) 

            do

                let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)

                if let stations = json["stations"] as? [[String: AnyObject]] 

                    for station in stations 

                        if let name = station["stationName"] as? String 

                            if let year = station["buildYear"] as? String 
                                self.TableData.append(name + "[" + year + "]")
                            

                        
                    

                

            catch 
                print("Error with Json: \(error)")

            

          /*  for element in TableData 
                print(element)
            */

        




    
    task.resume()



    //--------------------------------------------------------------------------------------------------


    tableView.delegate = self
    tableView.dataSource = self


override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.



func numberOfSectionsInTableView(tableView: UITableView) -> Int 
    return 1


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    return TableData.count


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath)

    let row = indexPath.row
    cell.textLabel?.text = TableData[row]

    return cell


 

在我的模拟器上只看到空表

【问题讨论】:

【参考方案1】:

这是因为您没有重新加载 tableView。 HTTP 请求需要一些时间才能完成。

一旦您的请求完成,请致电self.tableView.reloadData()

它将在另一个队列中,因此请使用

 NSOperationQueue.mainQueue().addOperationWithBlock  () -> Void in
      self.tableView.reloadData()
 

放置

// Stuff above...

if let stations = json["stations"] as? [[String: AnyObject]] 

    for station in stations 

        if let name = station["stationName"] as? String 

            if let year = station["buildYear"] as? String 
                self.TableData.append(name + "[" + year + "]")
            
        
    

    // Do your reload here.. as your array will have finished populating


// Stuff below.. 

【讨论】:

预期申报,如何申报? 听起来你把它放在了超出方法范围的地方 变量名也不要大写,TableData ...应该是tableData。您也可以这样声明 - var tableData = [String]() ... 更简洁 imo 编译,但仍为空

以上是关于ViewTable 不读取数组(IOS)的主要内容,如果未能解决你的问题,请参考以下文章

如何从 plist iOS 中读取数组

IOS - 目标 C,如何读取核心数据并将其存储到可变数组中

我如何在闭包之外访问数组?

用触动怎么读取plist的数组?

有没有办法从子数组中读取?

iOS开发,如何替换plist这个数组下数据