lundi 27 juin 2016

JSON result in Tableview in Swift showing list of results instead of each result in each cell (picture attached)

So I am making an app with Swift 2 (not 3.0) and I've come across a problem in which I've gotten the JSON to show up on my Tableview but it's showing all the results in each cell instead of one result in each cell. Here's a picture showing the problem Picture of problem

My MasterViewController:

import UIKit
import Alamofire
import SwiftyJSON

class MasterViewController: UITableViewController {

var tableTitle = [String]()
var tableBody = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    getJSON()

}

func getJSON() {

    Alamofire.request(.GET, "http://www.graydoncs.club/api/posts").responseJSON { (Response) in

        if let value = Response.result.value {

            let json = JSON(value)

            for anItem in json.array! {

                let title: String? = anItem["title"].stringValue
                self.tableTitle.append(title!)
                print(anItem["title"].stringValue)

            }

            dispatch_async(dispatch_get_main_queue()) {

                self.tableView.reloadData()

            }

        }

    }

}

override func viewWillAppear(animated: Bool) {
    self.clearsSelectionOnViewWillAppear = self.splitViewController!.collapsed
    super.viewWillAppear(animated)
}

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

// MARK: - Segues

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let object = tableTitle[indexPath.row] as! String
            let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
            controller.detailItem = object
            controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
            controller.navigationItem.leftItemsSupplementBackButton = true
        }
    }
}

// MARK: - Table View

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

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

    let object = tableTitle[indexPath.row] as! String
    cell.textLabel!.text = tableTitle.description
    return cell
}

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return false if you do not want the specified item to be editable.
    return false
}


}

Any help regarding this problem would be greatly appreciated. Thanks!

Aucun commentaire:

Enregistrer un commentaire