mercredi 22 avril 2015

Swift how to make my ViewController label display information corresponding to the information of a clicked tableView cell

I have a function that uses swifty JSON and calls a web service using my DataManager swift file. I'm using my AppModel.swift as a model to populate my array with information I need from that JSON string. I'm using that array to call back pieces of information I need to update my DetailsViewController's labels. Here is my function:

class EarthTableViewController: UITableViewController {
    var info = [AppModel]()

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

        DataManager.getEarthquakeDataFromFileWithSuccess {
            (data) -> Void in

                let json = JSON(data: data)

                if let JsonArray =  json.array {

                    for appDict in JsonArray {
                        var ids: String? = appDict["id"].stringValue
                        var title: String? = appDict["title"].stringValue
                        var time: String? = appDict["time"].stringValue
                        var information = AppModel(idEarth: ids, title: title, time: time)





One person on here helped me populate my tableView with title. When I segue from my EarthTableViewController's cell to my DetailsViewController, I want one of the labels to display the time of that corresponding cell. For example if my cell says California, when I click on it, it'll show the time corresponding to California's Earthquake. Is there anyone who can help me with what syntax I should use for this? Here is my prepareforsegue method:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

        if segue.identifier == "SEGUE" {
            let vc = segue.destinationViewController as DetailsViewController
            let cell = (sender as UITableViewCell)
             let title = cell.textLabel!.text
            vc.titleData = title
            let timeArray = //error [{AppModl}] does not have a member named 'time'


And finally here's my AppModel.swift:

class AppModel: NSObject, Printable {
    let idEarth: String
    let title: String
    let time: String

    override var description: String {
        return "ID: \(idEarth), TITLE: \(title), TIME: \(time), \n"

    init(idEarth: String?, title: String?, time: String?) {
        self.idEarth = idEarth ?? ""
        self.title = title ?? ""
        self.time = time ?? ""



class DetailsViewController: UIViewController {

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var idLabel: UILabel!

    @IBOutlet weak var timeLabel: UILabel!
    var titleData: String!
    var idData: String!
    var timeData: String!

    override func viewDidLoad() {
        var earthInfo = EarthTableViewController()
        var getEarthInfo: () = earthInfo.getEarthquakeInfo { (info) in


        titleLabel.text = titleData
        idLabel.text = idData
       timeLabel.text = timeData

Any help would be appreciated. If I should add more of my code, please let me know.

Aucun commentaire:

Enregistrer un commentaire