【问题标题】:Swift TableView doesn't return numbersOfRowSwift TableView 不返回 numbersOfRow
【发布时间】:2017-05-05 16:03:12
【问题描述】:
import UIKit
import SwiftyJSON

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{        
    @IBOutlet weak var tableView: UITableView!    
    var mass = [String]()
    var jDatas = datas()    
    override func viewDidLoad() {
        super.viewDidLoad()        
        let url = URL(string: "https://www.quandl.com/api/v3/datasets/WIKI/AAPL.json")
        let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
            if error != nil {
                print("error")
            } else {
                if let content = data {
                    let myJson = JSON(data: content)                    
                    for item in myJson["dataset"]["data"] {                        
                        let dates = "open \(String(describing: item.1[1].string)))"                 
                        self.mass.append(dates)                      
                    }       
                }
            }
        }
        task.resume()
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return mass.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SimpleTableViewCell
        cell.datasLabel?.text = "some text"
        return cell
    }
}

所以我的问题是 numberOfRowInSection 不起作用,我尝试了所有方法,有人可以说是什么问题吗?

当我尝试调试代码时,它说我的海量中有 0

【问题讨论】:

    标签: swift tableview swifty-json


    【解决方案1】:

    您忘记在 dataTask 的闭包中的 for 循环之后重新加载 tableView,所以只需在主线程上重新加载 tableView

    for item in myJson["dataset"]["data"] {                        
       let dates = "open \(String(describing: item.1[1].string)))"                 
       self.mass.append(dates)                      
    }
    DispatchQueue.main.async {
        self.tableView.reloadData()
    }
    

    【讨论】:

    • tnx,它的工作)但现在当我试图在 tableView 中使用我的大量时,它说 open nil)
    • @SerjSemenov 欢迎朋友,那是因为数组不包含字符串值,但它是数字尝试这样let dates = "open \(String(item.1[1].doubleValue)))"
    • 天哪,我没注意到,tnx 很多)
    猜你喜欢
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    相关资源
    最近更新 更多