mercredi 24 juin 2015

SWIFT sorting cells that have a collection of different objects by a specific object

So basically, I have a function that calls a JSON web service, and uses SwiftyJSON to collect all the info I need to populate my tableview, as well as other info I need for my DetailViewController. Some examples of the information I collected were the title of the entry, time, ID, and magnitude. I would like to know how I can sort my tableView so that the first cell shows the highest magnitude, all the way down to the last cell showing lowest magnitude.

I've been searching for a way to do this, but I hadn't had much luck because the way the JSON is formatted, it's an array of 50 dictionaries. My collection of Magnitudes are 50 strings, not in an array.

Here's my code:

  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

    }

If anyone could just point me in the right direction on how to do this, I'd appreciate it.

See more ..

Aucun commentaire:

Enregistrer un commentaire