SWIFT对特定对象具有不同对象集合的单元格进行排序

时间:2015-06-24 20:30:00

标签: swift uitableview sorting

基本上,我有一个调用JSON Web服务的函数,并使用SwiftyJSON来收集填充tableview所需的所有信息,以及我需要的DetailViewController所需的其他信息。我收集的信息的一些例子是条目的标题,时间,ID和幅度。我想知道如何对tableView进行排序,以便第一个单元格显示最高的大小,一直到最后一个单元格显示最低大小。

我一直在寻找一种方法来做到这一点,但由于JSON的格式化方式,我没有太多运气,它是一个包含50个词典的数组。我的Magnitude系列是50个字符串,而不是数组。

这是我的代码:

  var info = [AppModel]()



    func getEarthquakeInfo(completion: (results : NSArray?) ->Void ){


        DataManager.getEarthquakeDataFromFileWithSuccess {
            (data) -> Void in

                let json = JSON(data: data)


                if var JsonArray =  json.array {
                    JsonArray.removeAtIndex(0)

                    for appDict in JsonArray {
                        // parsing
                        var ids: String? = appDict["id"].stringValue
                        var title: String? = appDict["title"].stringValue
                        var time: String? = appDict["time"].stringValue
                        var lattitude: String? = appDict["lat"].stringValue
                        var longitude: String? = appDict["lng"].stringValue
                        var north: String? = appDict["north"].stringValue
                        var west: String? =  appDict["west"].stringValue
                        var mag: String? = appDict["mag"].stringValue
                        var depth: String? = appDict["depth"].stringValue
                        var timeStamp: String? = appDict["timestamp"].stringValue
                        // Splitting up title string into 2 parts
                        let newString = title!.stringByReplacingOccurrencesOfString("  ", withString: " - ", options: NSStringCompareOptions.LiteralSearch, range: nil)
                        var title2strings = newString.componentsSeparatedByString(" - ")
                        var scale = title2strings[0]
                        var location = title2strings[1]
                        // replacing M in scale string with Richter Scale
                        let scaleString = scale.stringByReplacingOccurrencesOfString("ML", withString: "Magnitude", options: NSStringCompareOptions.LiteralSearch, range: nil)
                        let scaleString2 = scaleString.stringByReplacingOccurrencesOfString("mb", withString: "Magnitude", options: NSStringCompareOptions.LiteralSearch, range: nil)
                        let scaleString3 = scaleString2.stringByReplacingOccurrencesOfString("Mw", withString: "Magnitude", options: NSStringCompareOptions.LiteralSearch, range: nil)
                         let scaleString4 = scaleString3.stringByReplacingOccurrencesOfString("MD", withString: "Magnitude", options: NSStringCompareOptions.LiteralSearch, range: nil)
                        let scaleString5 = scaleString4.stringByReplacingOccurrencesOfString("M", withString: "Magnitude", options: NSStringCompareOptions.LiteralSearch, range: nil)
                        //Formatting the date
                        var date = NSDate(dateString: time!).getDatePart()
                        // sorting

                        // Collecting all the information
                        var information = AppModel(idEarth: ids, title: title, time: date, lat: lattitude, lng: longitude, north: north!, west: west, mag: mag, depth: depth, timeStamp: timeStamp, location: location, scale: scaleString5)


                        self.info.append(information)
                        completion(results: self.info)
                    }


            }

        }

    }




    override func viewDidLoad() {
        super.viewDidLoad()
        //register custom cell

        getEarthquakeInfo { (info) in
            self.tableView.reloadData()


            dispatch_async(dispatch_get_main_queue()) {
                self.tableView.reloadData()

            }
        }


    }




    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->   UITableViewCell {
        let cell: TheCustomCellTableViewCell = tableView.dequeueReusableCellWithIdentifier("CELL", forIndexPath: indexPath) as TheCustomCellTableViewCell
        cell.Location.text = info[indexPath.row].location
        cell.Scale.text = info[indexPath.row].scale
        cell.Time.text = info[indexPath.row].time
        return cell

    }

如果有人能指出我正确的方向,我会很感激。

1 个答案:

答案 0 :(得分:2)

如何在返回完成处理程序之前对MapModel数组进行排序?

self.info.sort({$0.mag > $1.mag})
completion(results: self.info)