【发布时间】:2016-07-09 01:57:44
【问题描述】:
我正在开发一个使用 Swift 的 iOS 项目。这个应用程序涉及在 UITableView 中显示 JSON 数据。
我已经解析了 JSON 数据,但它没有显示在 tableview 上。当我运行我的应用程序时,它会给我这个错误:
由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',原因:'无法使单元格出列 带有标识符 Cell - 必须为 标识符或连接故事板中的原型单元'
我正在尝试以编程方式完成所有事情,而不是使用情节提要。 我将在下面添加我的代码。
import UIKit
import Foundation
class TableViewController: UITableViewController
{
var postsCollection = [Post]()
var service: PostService!
override func awakeFromNib()
{
super.awakeFromNib()
}
override func viewDidLoad()
{
super.viewDidLoad()
service = PostService()
service.getPosts
{
(response) in
self.loadPosts(response["Events"]! as! NSArray)
}
}
func loadPosts(events: NSArray)
{
for event in events
{
//var event = event["Events"]! as! NSDictionary
print("\n-----------------------------------------------------")
print(" RECEIVED_JSON_DATA")
print("-----------------------------------------------------")
print("Title : \(event["Title"])")
print("Event Description : \(event["EventDescription"])")
print("Event Location : \(event["EventLocation"])")
print("Event StartTime : \(event["EventStartTime"])")
print("Event EndTime : \(event["EventEndTime"])")
print("RowKey : \(event["RowKey"])")
print("-----------------------------------------------------\n")
var title = event["Title"]! as! String
var description = event["EventDescription"]! as! String
var location = event["EventLocation"]! as! String
var startTime = event["EventStartTime"]! as! String
var endTime = event["EventEndTime"]! as! String
//var eventURL = event["EventURL"]! as! String
var rowKey = event["RowKey"]! as! String
//var postObj = Post(title: title, description: description, location: location, startTime: startTime, endTime: endTime, eventURL: eventURL, rowKey: rowKey)
var postObj = Post(title: title, description: description, location: location, startTime: startTime, endTime: endTime, rowKey: rowKey)
postsCollection.append(postObj)
}
dispatch_async(dispatch_get_main_queue())
{
self.tableView.reloadData()
}
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// TableView
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return postsCollection.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let object = postsCollection[indexPath.row]
cell.textLabel!.text = object.title
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 true
}
}
谁能帮我解决这个问题并告诉我哪里出错了?
谢谢!
【问题讨论】:
-
就像我说的,我试图以编程方式完成所有事情,而不是使用 Storyboard。有没有其他不使用故事板或 xib 的方法?
-
其实错误信息告诉你怎么做:必须注册一个nib或者标识符的一个类
-
当单元格原型未添加到 IB 中的 tableView 或 collectionView 出口时。 必须在出列之前注册单元 Nib :)
标签: ios json swift uitableview