【发布时间】: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