【问题标题】:Can't show list of parsed JSON into TableView无法在 TableView 中显示解析的 JSON 列表
【发布时间】:2015-12-30 11:23:24
【问题描述】:

我是 swift 新手,不知道为什么 UITableView 没有显示来自 JSON 数组的动态数据。 我想通过使用swiftyJSON从iTunes中获取顶级应用列表,然后根据解析的数据创建一个动态表。 我的问题是,当我运行这段代码时,我得到的所有代码都是 5 行,其中的值相同,如下图所示

我怎样才能让它变得动态,我在哪里弄错了? 提前致谢。

更新代码:

import UIKit



struct Apps {
    var name : String!
}

class createTable: UITableViewController {

     var tableData = [Apps]()

    //Mark Properties
    @IBOutlet var appTableView : UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()


        //Get the #1 app name from iTunes and SwiftyJSON
        DataManager.getTopAppsDataFromItunesWithSuccess { (iTunesData) -> Void in
            let json = JSON(data: iTunesData)


            if let appArray = json["feed"]["entry"].array {
                for appDict in appArray {
                    let appName : String! = appDict["im:name"]["label"].string
                     let ap = Apps(name: appName)
                    self.tableData.append(ap)

                }
            }
        }

        self.tableView.reloadData()
    }



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



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


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


           override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell  {

                //Table view cells are reused and should be dequeued using a cell identifier.
                let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

                    let rowData = self.tableData[indexPath.row]
                    cell.textLabel!.text = rowData.name


            return cell
        }


}

输出:

【问题讨论】:

  • 你设置了数据源和委托吗?为什么在表格单元格行委托方法中调用DataManager.getTopAppsDataFromItunesWithSuccess?试着在外面打电话。

标签: ios json swift uitableview swifty-json


【解决方案1】:

不要请求cellForRowAtIndexPath 中的数据。请求数据,例如viewDidLoadtrigger 一个 reload 的 tableview。

创建一个包含所需数据的自定义类,创建这些自定义类的数组,为每个单元格使用该自定义类的一个元素。

什么是

var appName : String!
if let appArray = json["feed"]["entry"].array {
    for appDict in appArray {
         appName = appDict["im:name"]["label"].string
    }
}
cell.textLabel!.text = appName

应该怎么做?您为 每个 单元格运行该代码。您总是多次分配appNameappName 将始终是找到的姓氏。因此所有标签都将获得相同的文本集。

解决方案总结。

  • 创建一个类App,其中包含一个属性name(稍后会详细介绍)
  • 在您的viewDidLoad 中向 AppStore 请求数据
  • 解析检索到的数据,创建App 的实例,每个都设置它们的名称,将这些实例存储在数组var apps = [App]()
  • 触发重新加载 tableView
  • 在您的cellForRowAtIndexPath 中检索与单元格索引对应的App
  • return apps.count 在您的 numberOfRowsInSection

【讨论】:

  • 感谢您的出色解决方案,但我可以在我当前的类 createTable 旁边创建一个类: UITableViewController 吗?并在我当前的课程中使用它?
  • @anonymox 因为这很快,所以在哪里创建新类并不重要,可以是同一个文件,也可以是不同的文件。没关系。
  • 所以你说删除foreach循环并将检索到的数据发送到tableview以显示它?
  • 你可以聊天吗?
  • @anonymox 你没有发送任何东西。您将检索到的数据存储在数组中。然后你打电话给tableView.reloadData()。不,不聊天。
【解决方案2】:

应该在闭包内部调用重新加载。 DataManager异步获取应用列表,解析数据后不调用刷新

    override func viewDidLoad() {
    super.viewDidLoad()


    //Get the #1 app name from iTunes and SwiftyJSON
    DataManager.getTopAppsDataFromItunesWithSuccess { (iTunesData) -> Void in
        let json = JSON(data: iTunesData)

        if let appArray = json["feed"]["entry"].array {
            for appDict in appArray {
                let appName : String! = appDict["im:name"]["label"].string
                let ap = Apps(name: appName)
                self.tableData.append(ap)

            }
        }

        tableView.reloadData()
    }

}

【讨论】:

    【解决方案3】:

    我将结果代码发布给那些想要使用它的人。

    代码:

    //
    //  createTable.swift
    //  TopApps
    
    
    import UIKit
    
    
    
    struct Apps {
        var name : String!
    }
    
    class createTable: UITableViewController {
    
         var tableData = [Apps]()
    
        //Mark Properties
        @IBOutlet var appTableView : UITableView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
    
            //Get the #1 app name from iTunes and SwiftyJSON
            DataManager.getTopAppsDataFromItunesWithSuccess { (iTunesData) -> Void in
                let json = JSON(data: iTunesData)
    
    
                if let appArray = json["feed"]["entry"].array {
                    for appDict in appArray {
                        let appName : String! = appDict["im:name"]["label"].string
                         let ap = Apps(name: appName)
                        self.tableData.append(ap)
    
                        self.tableView.reloadData()
    
                    }
                }
            }
    
    
        }
    
    
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
    
            override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
                return 1
            }
    
    
               override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                    return self.tableData.count
                }
    
    
               override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell  {
    
                    //Table view cells are reused and should be dequeued using a cell identifier.
                    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
    
                        let rowData = self.tableData[indexPath.row]
                        cell.textLabel!.text = rowData.name
    
    
                return cell
            }
    
    
    }
    

    输出:

    【讨论】:

    • 在“for ... In”循环后重新加载表格视图,在您当前的实现中,您在每个循环周期都重新加载表格视图。见我上面的回答:)
    • @DeVladinci 我试过你的方法,但没用,屏幕上出现空表视图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 2016-07-26
    • 1970-01-01
    相关资源
    最近更新 更多