我正在使用 Swift 开发一个 iOS 项目。此应用涉及在 UITableView 中显示 JSON 数据。
我已经解析了 JSON 数据,但它没有显示在表格 View 中。每当我运行我的应用程序时,它都会给我这个错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
我正在尝试以编程方式完成所有操作,而不是使用 Storyboard。 我将在下面添加我的代码。
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
}
}
谁能帮我解决这个问题并告诉我哪里出错了?
谢谢!
最佳答案
确保在某处(可能在 TableViewController
的 viewDidLoad
中)注册单元格类:
swift 4:
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
Swift 3 及以下版本:
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
关于ios - swift -UITableView 'unable to dequeue a cell with identifier Cell',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36150011/