【问题标题】:JSON result in Tableview in Swift showing list of results instead of each result in each cell (picture attached) [closed]JSON 结果在 Swift 中的 Tableview 中显示结果列表,而不是每个单元格中的每个结果(附图片)[关闭]
【发布时间】:2016-06-26 19:03:08
【问题描述】:

所以我正在使用 Swift 2(不是 3.0)制作一个应用程序,我遇到了一个问题,我让 JSON 显示在我的 Tableview 上,但它在每个单元格中显示所有结果而不是一个结果在每个单元格中。这是一张显示问题的图片

我的主视图控制器:

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
}


}

任何有关此问题的帮助将不胜感激。谢谢!

【问题讨论】:

  • 提示: 分配给let object = ... 的对象根本没有被使用。 – 是否没有编译器警告“从未使用过不可变值‘对象’的初始化”?

标签: ios json swift uitableview


【解决方案1】:
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 = object
return cell 

}

【讨论】:

    猜你喜欢
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    相关资源
    最近更新 更多