Ios Swift中Tableview中的JSON结果显示结果列表,而不是每个单元格中的每个结果(附图)
所以我用Swift 2(不是3.0)制作了一个应用程序,我遇到了一个问题,我让JSON显示在我的Tableview上,但它显示每个单元格中的所有结果,而不是每个单元格中的一个结果。这是一张显示问题的图片 我的MasterViewController:Ios Swift中Tableview中的JSON结果显示结果列表,而不是每个单元格中的每个结果(附图),ios,json,swift,uitableview,Ios,Json,Swift,Uitableview,所以我用Swift 2(不是3.0)制作了一个应用程序,我遇到了一个问题,我让JSON显示在我的Tableview上,但它显示每个单元格中的所有结果,而不是每个单元格中的一个结果。这是一张显示问题的图片 我的MasterViewController: import UIKit import Alamofire import SwiftyJSON class MasterViewController: UITableViewController { var tableTitle = [Stri
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=…
的对象根本不被使用是否没有编译器警告“从未使用过不可变值“object”的初始化”?
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
}