簡體   English   中英

在表格視圖中不顯示數據

[英]not showing data in table view swift

我有一個帶有兩個標簽的表格視圖。 我需要顯示來自json的數據。 但是現在它沒有在表視圖中顯示任何數據:

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate
{
    let yourJsonFormat: String = "JSONFile" // set text JSONFile : json data from file
                                            // set text JSONUrl : json data from web url

    var arrDict :NSMutableArray=[]

    @IBOutlet weak var tvJSON: UITableView!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        if yourJsonFormat == "JSONFile" {
            jsonParsingFromFile()
        } else {
            jsonParsingFromURL()
        }
    }

    func jsonParsingFromURL () {
        let url = NSURL(string: "url")
        let request = NSURLRequest(URL: url!)

        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in

        }
    }

    func jsonParsingFromFile()
    {
        let path: NSString = NSBundle.mainBundle().pathForResource("days", ofType: "json")!
        let data : NSData = try! NSData(contentsOfFile: path as String, options: NSDataReadingOptions.DataReadingMapped)


    }

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

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return arrDict.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell : TableViewCell! = tableView.dequeueReusableCellWithIdentifier("Cell") as! TableViewCell
        let strTitle : NSString=arrDict[indexPath.row] .valueForKey("name") as! NSString
        let strDescription : NSString=arrDict[indexPath.row] .valueForKey("rating") as! NSString
        cell.lblTitle.text=strTitle as String
        cell.lbDetails.text=strDescription as String
        return cell as TableViewCell
    }
}

我錯過的任何事情,請幫幫我。

我在表格視圖中看不到任何數據...

您的代碼部分正確,我按照您的問題

第1步

右鍵單擊info.plist文件,選擇“打開方式”,“源代碼”。 添加允許http連接到該服務器的代碼行。

在此處輸入圖片說明

喜歡

第2步

對於服務器請求

sendAsynchronousRequest在此地方不建議使用

 func jsonParsingFromURL () {
    let url = NSURL(string: "url")
    let session = NSURLSession.sharedSession()

let request = NSURLRequest(URL: url!)
let dataTask = session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
  print("done, error: \(error)")
  let dict: NSDictionary!=(try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary 
  arrDict.addObject((dict.valueForKey("xxxx") 
  tvJSON .reloadData()

}
dataTask.resume()


}

對於本地要求

func jsonParsingFromFile()
  {
    let path: NSString = NSBundle.mainBundle().pathForResource("days", ofType: "json")!
    let data : NSData = try! NSData(contentsOfFile: path as String, options: NSDataReadingOptions.DataReadingMapped)
       let dict: NSDictionary!=(try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary 
  arrDict.addObject((dict.valueForKey("xxxx") 
  tvJSON .reloadData()

}

更新和編輯

  class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

@IBOutlet var showtable: UITableView!
 var arrDict :NSMutableArray=[]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

     self.jsonParsingFromURL()
}

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

func jsonParsingFromURL () {
    let url = NSURL(string: "http://kirisoft.limitscale.com/GetVendor.php?category_id=1")
    let session = NSURLSession.sharedSession()

    let request = NSURLRequest(URL: url!)
    let dataTask = session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
        print("done, error: \(error)")
        if error == nil
        {
        self.arrDict=(try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSMutableArray

        print(self.arrDict)

            if (self.arrDict.count>0)
            {

                self.showtable.reloadData()
            }

       // arrDict.addObject((dict.valueForKey("xxxx")
        }


    }
    dataTask.resume()


}

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return arrDict.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let CellIdentifier: String = "cell"

    var cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier) as UITableViewCell!

    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: CellIdentifier)
    }


    cell?.textLabel!.text=arrDict[indexPath.row] .valueForKey("name")   as? String
     cell?.detailTextLabel!.text=arrDict[indexPath.row] .valueForKey("rating")   as? String

    return cell!
}

}

你可以獲得像這樣的輸出

在此處輸入圖片說明

對於樣本項目

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM