【问题标题】:sorting SwiftyJSON results对 SwiftyJSON 结果进行排序
【发布时间】:2016-02-13 15:47:31
【问题描述】:

我正在使用 SwiftyJSON 填充工作正常的表格视图,但我正在努力寻找一种对数据进行排序的方法。我已经放置了我的代码,因为我感觉有一种更好的方法来存储和显示数据,因为目前我将它放入每个 json 标记的单独数组中,这使得排序变得困难。对 swift 来说非常新,因此非常感谢您的帮助。我可以在使用之前对 json 结果进行排序,还是有更好的存储方式?

我想根据打印到表格视图的时间对下面的内容进行排序,因为它只是按顺序打印。

示例 json:

[
  {
    "name": "John Doe",
    "time": 13683
  },
  {
    "name": "Dave Smith",
    "time": 20683
  },
  {
    "name": "Craig David",
    "time": 200
  }
]

当前方法(无排序):

// Global variables
var tableName = [String]()
var tableTime = [String]()

func getJSON(){
    // Removed all the code here to get the JSON 

    let json = JSON(data: result!)

    dispatch_async(dispatch_get_main_queue(), {
        for item in json.arrayValue {
            if item["name"].stringValue != "" {
                self.tableName.append(item["name"].stringValue )
                self.tableTime.append(item["time"].stringValue)
            }
        }
        dispatch_async(dispatch_get_main_queue(),{
            self.tableView.reloadData()
        })
    })
}

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
    UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as!TableViewCell
        // Configure the cell...

        cell.name.text = tableName[indexPath.row]
        cell.time.text = tableTime[indexPath.row]

        return cell

    }  
}

【问题讨论】:

    标签: ios swift swifty-json


    【解决方案1】:

    使用自定义结构作为数据模型

    struct Data {
        var name : String
        var time : String
    }
    

    那么你只有一个数组要排序

    // Global variables
    var dataSource = [Data]()
    
    func getJSON(){
        // Removed all the code here to get the JSON 
    
        let json = JSON(data: result!)
        for item in json.arrayValue {
            let name = item["name"].stringValue
            if !name.isEmpty {
                self.dataSource.append(Data(name:name, time:item["time"].stringValue))
            }
        }
        self.dataSource.sortInPlace{ $0.name < $1.name}
        dispatch_async(dispatch_get_main_queue(),{
            self.tableView.reloadData()
        })
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return dataSource.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
        UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as!TableViewCell
            // Configure the cell...
    
            let data = dataSource[indexPath.row]
            cell.name.text = data.name
            cell.time.text = data.time
    
            return cell
    
        }  
    }
    

    【讨论】:

    • 谢谢,这是一种更好的存储方式,但看起来这种排序方式不适合我。我已将排序从名称更改为时间,并在结构中将时间设置为 Int 并追加,但它没有排序。
    • 抱歉,我好像搞砸了,一切正常!非常感谢。
    猜你喜欢
    • 2020-01-27
    • 2018-07-25
    • 1970-01-01
    • 2014-01-08
    • 2012-02-11
    • 2014-12-24
    • 2016-01-25
    • 2016-04-02
    • 2021-06-29
    相关资源
    最近更新 更多