IOS swift中的“NSUnknownKeyException”
我不熟悉使用swift进行IOS开发。现在我尝试将UItableViewController与多个不同的单元格一起使用。但是,多个单元格使用相同的数据源。 这是我在tableviewController类中的代码IOS swift中的“NSUnknownKeyException”,ios,swift,uitableview,Ios,Swift,Uitableview,我不熟悉使用swift进行IOS开发。现在我尝试将UItableViewController与多个不同的单元格一起使用。但是,多个单元格使用相同的数据源。 这是我在tableviewController类中的代码 import UIKit class plantTableViewController: UITableViewController { var plantsHelper = [PlantsHelper]() override func viewDidLoad() { s
import UIKit
class plantTableViewController: UITableViewController {
var plantsHelper = [PlantsHelper]()
override func viewDidLoad() {
super.viewDidLoad()
//add data
loadSample()
}
func loadSample()
{
let photo1 = UIImage(named: "bestplace")!
let plantsHelper1 = PlantsHelper(name: "Best Place", photo: photo1, comment: "Find out the best place in your home to plant a plants!")!
let photo2 = UIImage(named: "recommandplant")!
let plantsHelper2 = PlantsHelper(name: "Recommanded Plants", photo: photo2, comment: "Find out what the fantastic plant we recommand!")!
let photo3 = UIImage(named: "monitorplant")!
let plantsHelper3 = PlantsHelper(name: "Monitor your plants", photo: photo3, comment: "Monitor your plants to give your plants a better environment!")!
plantsHelper += [plantsHelper1, plantsHelper2, plantsHelper3]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return 3
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Configure the cell...
let plantHelper = plantsHelper[indexPath.row]
if indexPath == 0
{
let cellIdentifier = "BestPlaceCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! BestPlaceCell
cell.bestPlaceNameLabel.text = plantHelper.name
cell.bestPlacePhoto.image = plantHelper.photo
cell.bestPlaceCommentLabel.text = plantHelper.comment
return cell
}
else if indexPath == 1
{
let cellIdentifier2 = "RecommandCell"
let cell2 = tableView.dequeueReusableCellWithIdentifier(cellIdentifier2, forIndexPath: indexPath) as! RecommandCell
cell2.recommandName.text = plantHelper.name
cell2.recommandComment.text = plantHelper.comment
cell2.recommandPhoto.image = plantHelper.photo
return cell2
}
else
{
let cellIdentifier3 = "MonitorCell"
let cell3 = tableView.dequeueReusableCellWithIdentifier(cellIdentifier3, forIndexPath: indexPath) as! MonitorCell
cell3.monitorName.text = plantHelper.name
cell3.monitorComment.text = plantHelper.comment
cell3.monitorPhoto.image = plantHelper.photo
return cell3
}
}
}
这是错误消息
2015-11-05 14:34:50.142 HousePlantHelper[15177:2412448] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<HousePlantHelper.MonitorCell 0x7fac485484d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key bestPlaceCommentLabel.'
*** First throw call stack:
(
0 CoreFoundation 0x0000000105f3fc65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000107aaabb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000105f3f8a9 -[NSException raise] + 9
3 Foundation 0x000000010635db53 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
4 UIKit 0x000000010685e67e -[UIView(CALayerDelegate) setValue:forKey:] + 149
5 CoreFoundation 0x0000000105e87d50 -[NSArray makeObjectsPerformSelector:] + 224
6 UIKit 0x0000000106ab64eb -[UINib instantiateWithOwner:options:] + 1506
7 UIKit 0x00000001068d1f7f -[UITableView _dequeueReusableViewOfType:withIdentifier:] + 308
8 UIKit 0x00000001068d21f5 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 42
9 HousePlantHelper 0x0000000105948a83 _TFC16HousePlantHelper24plantTableViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 3363
10 HousePlantHelper 0x0000000105948fdf _TToFC16HousePlantHelper24plantTableViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 79
11 UIKit 0x00000001068df9e8 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
12 UIKit 0x00000001068be208 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2853
13 UIKit 0x00000001068d4869 -[UITableView layoutSubviews] + 210
14 UIKit 0x000000010685e9eb -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
15 QuartzCore 0x000000010a62ced2 -[CALayer layoutSublayers] + 146
16 QuartzCore 0x000000010a6216e6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
17 QuartzCore 0x000000010a621556 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
18 QuartzCore 0x000000010a58d86e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
19 QuartzCore 0x000000010a58ea22 _ZN2CA11Transaction6commitEv + 462
20 UIKit 0x00000001067dc9ed -[UIApplication _reportMainSceneUpdateFinished:] + 44
21 UIKit 0x00000001067dd6b1 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2648
22 UIKit 0x00000001067dc095 -[UIApplication workspaceDidEndTransaction:] + 179
23 FrontBoardServices 0x00000001095a75e5 __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 21
24 CoreFoundation 0x0000000105e7341c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
25 CoreFoundation 0x0000000105e69165 __CFRunLoopDoBlocks + 341
26 CoreFoundation 0x0000000105e68947 __CFRunLoopRun + 887
27 CoreFoundation 0x0000000105e68366 CFRunLoopRunSpecific + 470
28 UIKit 0x00000001067dbb02 -[UIApplication _run] + 413
29 UIKit 0x00000001067de8c0 UIApplicationMain + 1282
30 HousePlantHelper 0x0000000105954e47 main + 135
31 libdyld.dylib 0x00000001081e0145 start + 1
32 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
似乎是为每一行设置数据的问题。但我不知道我的代码有什么问题
有人能解决这个问题吗?用完整准确的错误信息更新您的问题。并指出导致问题的原因。是否所有这些单元格都在故事板中设置为原型,并指定了相应的自定义UITableViewCell子类,以及您在代码中使用的相同重用标识符?代码的哪一行引发异常?所有单元格都设置在情节提要中,并且代码中的重用标识符不同……首先,您的if's for indexPath将始终解析为MonitorCell,因为indexPath从不为0或1。您需要检查indexPath.row。其次,错误表示MonitorCell没有属性bestPlaceCommentLabel。您的MonitorCell类是否具有该属性?