簡體   English   中英

數據未顯示在表格視圖中

[英]Data is not showing in table view

我是 iOS 新手,從一些教程開始。 但是當我嘗試在tableview顯示內容時,它沒有顯示。 我做了他們在那個教程中所做的一切。 但是我仍然無法在我的表格視圖中顯示我的內容。

這是我的教程鏈接:我的鏈接

這是我的代碼:

import UIKit
import Alamofire

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!
    var nameArray = [AnyObject]()
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.


        Alamofire.request("http://thecodeeasy.com/test/swiftjson.json").responseJSON { response in

            let result = response.result
            if let dict = result.value as? Dictionary<String,AnyObject>{
                if let mainDict = dict["actors"] {
                    self.nameArray = mainDict as! [AnyObject]
                   self.tableView.reloadData()
                }

            }


             }
    }



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

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) as? CustomTableViewCell

        let title = nameArray[indexPath.row]["title"]
        cell?.ContentName.text = title as? String
        return cell!
    }

}

您沒有獲取數據的三種可能性。 首先,您的應用數據源設置不正確。 請檢查圖像

在此處輸入圖片說明

其次,將您的 API 代碼粘貼到 viewDidAppear 並檢查它是否有效

============== 重要 ============

第三,檢查您的 info.plist 包含以下代碼。 因為每當您撥打互聯網電話時,您都應該將此代碼放入您的 info.plist 文件中。

右鍵單擊 info.plist 並作為源代碼打開

粘貼以下代碼

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

嘗試運行該應用程序。 讓我知道它是否適合你。

請檢查代碼的鏈接點擊這里

謝謝

阿布舍克·夏爾馬

在 viewDidLoad 頂部試試這個

self.tableView.delegate = self
self.tableView.dataSource = self

正如我所看到的,您正在視圖控制器中使用tableview 您應該將此tableview委托和數據源設置為self

在你的視圖控制器中寫下這兩行:

self.tableView.delegate = self
self.tableView.datasource = self

你也可以用 XIB 做到這一點。

如果你已經這樣做了或者那不起作用,那么嘗試打印你的self.nameArray並檢查是否有預期的數據。

暫無
暫無

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

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