我正在创建一个表格应用程序(来自AppCoda的教程书),但是当我运行程序时出现错误Thread 1: signal SIGABRT
。调试区域说:
2015-10-26 14:47:34.827 FoodPin[4232:291494] Unknown class _TtC1q23RestaurantTableViewCell in Interface Builder file.
2015-10-26 14:47:34.855 FoodPin[4232:291494] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UITableViewCell 0x7f8062ca81d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key locationLabel.'
*** First throw call stack:
(
0 CoreFoundation 0x0000000104a8df65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010671fdeb objc_exception_throw + 48
2 CoreFoundation 0x0000000104a8dba9 -[NSException raise] + 9
3 Foundation 0x0000000104e55f5b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288
4 UIKit 0x0000000105358c4b -[UIView(CALayerDelegate) setValue:forKey:] + 173
5 UIKit 0x0000000105648923 -[UIRuntimeOutletConnection connect] + 109
6 CoreFoundation 0x00000001049ceb10 -[NSArray makeObjectsPerformSelector:] + 224
7 UIKit 0x0000000105647306 -[UINib instantiateWithOwner:options:] + 1864
8 UIKit 0x00000001053e52a5 -[UITableView _dequeueReusableViewOfType:withIdentifier:] + 388
9 UIKit 0x00000001053e5677 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 71
10 FoodPin 0x00000001048a5d85 _TFC7FoodPin29RestaurantTableViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 197
11 FoodPin 0x00000001048a646f _TToFC7FoodPin29RestaurantTableViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 79
12 UIKit 0x00000001053f76b3 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 782
13 UIKit 0x00000001053f77c8 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
14 UIKit 0x00000001053cd650 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3187
15 UIKit 0x0000000105400595 -[UITableView _performWithCachedTraitCollection:] + 92
16 UIKit 0x00000001053e89ad -[UITableView layoutSubviews] + 218
17 UIKit 0x000000010535911c -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
18 QuartzCore 0x000000010976036a -[CALayer layoutSublayers] + 146
19 QuartzCore 0x0000000109754bd0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
20 QuartzCore 0x0000000109754a4e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
21 QuartzCore 0x00000001097491d5 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
22 QuartzCore 0x00000001097769f0 _ZN2CA11Transaction6commitEv + 508
23 QuartzCore 0x0000000109777154 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
24 CoreFoundation 0x00000001049b99d7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
25 CoreFoundation 0x00000001049b9947 __CFRunLoopDoObservers + 391
26 CoreFoundation 0x00000001049aeebc CFRunLoopRunSpecific + 524
27 UIKit 0x00000001052a398d -[UIApplication _run] + 402
28 UIKit 0x00000001052a8676 UIApplicationMain + 171
29 FoodPin 0x00000001048abb0d main + 109
30 libdyld.dylib 0x000000010723792d start + 1
31 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
这是我的代码:
自定义视图控制器:
import UIKit
class RestaurantTableViewController: UITableViewController {
var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh's Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "Thai Cafe"]
var restaurantImages = ["cafedeadend.jpg", "homei.jpg", "teakha.jpg", "cafeloisl.jpg", "petiteoyster.jpg", "forkeerestaurant.jpg", "posatelier.jpg", "bourkestreetbakery.jpg", "haighschocolate.jpg", "palominoespresso.jpg", "upstate.jpg", "traif.jpg", "grahamavenuemeats.jpg", "wafflewolf.jpg", "fiveleaves.jpg", "cafelore.jpg", "confessional.jpg", "barrafina.jpg", "donostia.jpg", "royaloak.jpg", "thaicafe.jpg"]
var restaurantLocations = ["Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Sydney", "Sydney", "Sydney", "New York", "New York", "New York", "New York", "New York", "New York", "New York", "London", "London", "London", "London"]
var restaurantTypes = ["Coffee & Tea Shop", "Cafe", "Tea House", "Austrian / Casual Drink", "French" ,"Bakery", "Bakery", "Chocolate", "Cafe", "American / Seafood", "American", "American", "Breakfast & Brunch", "Coffee & Tea", "Coffee & Tea", "Latin American", "Spanish", "Spanish", "Spanish", "British", "Thai"]
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return restaurantNames.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! RestaurantTableViewCell
// Configure the cell...
cell.nameLabel.text = restaurantNames[indexPath.row]
cell.locationLabel.text = restaurantLocations[indexPath.row]
cell.typeLabel.text = restaurantTypes[indexPath.row]
cell.thumbnailImageView.image = UIImage(named: restaurantImages[indexPath.row])
return cell
}
自定义单元格:
import UIKit
class RestaurantTableViewCell: UITableViewCell {
@IBOutlet var nameLabel: UILabel!
@IBOutlet var locationLabel: UILabel!
@IBOutlet var typeLabel: UILabel!
@IBOutlet var thumbnailImageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
我的所有插座都连接到正确的标签/图像。我尝试连接和断开它们,但这没有用。
这是我的Main.storyboard:
答案 0 :(得分:2)
此类不是密钥locationLabel的密钥值编码兼容。
这意味着您的locationLabel
插座未设置。所以你的代码崩溃了,因为它试图访问一个nil的变量(在这种情况下是一个出口)。
答案 1 :(得分:0)
如果没有看到异常的开始,我最好的猜测是你没有在你的故事板中为你的单元格提供正确的reuseIdentifiers
,或者你没有将单元格设置为你的自定义类。