首先,这个讨论没有解决我的问题。
自定义 UITableViewCell 子类:该类不符合键值编码
设置:
我在
MainViewController
中有一个 Person 对象数组。
想要在 UITableView
中显示 AllContactsViewController
中的对象。
问题:
使用默认值
UITableViewCell
时,一切都按预期工作。
当我使用自定义的 TableCell
时,我收到一个错误,指向
该类不符合键值编码。
当我将
outlets
中的三个 IB
中的任何一个与我的 TableCell
类连接后,就会发生此错误。
注:
TableCell
具有三个属性:name
和 email
标签 + imageView
。
希望能帮到你。
TableCell.h
#import <UIKit/UIKit.h>
@interface TableCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *emailLabel;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
TableCell.m
#import "TableCell.h"
@implementation TableCell
@synthesize nameLabel, imageView, emailLabel;
AllContactsViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdent = @"TableCell";
TableCell *cell = (TableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdent];
if (cell == nil) {
NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"TableCell" owner:self options:nil];
cell = [array objectAtIndex:0];
}
NSString *firstAndLastnameString =
[NSString stringWithFormat:@"%@ %@",
[(NewContact *)[self.allContactsArray objectAtIndex:[indexPath row]] firstName],
[(NewContact *)[self.allContactsArray objectAtIndex:[indexPath row]] lastName]];
cell.nameLabel.text = firstAndLastnameString;
cell.emailLabel.text =
[(NewContact *)[self.allContactsArray objectAtIndex:[indexPath row]] eMail];
cell.imageView.image =
[(NewContact *)[self.allContactsArray objectAtIndex:[indexPath row]] contactPicture];
return cell;
}
更新:
也许 IB 提供的屏幕截图会有帮助。
添加了完整的 AllContactsViewController.h
#import <UIKit/UIKit.h>
#import "VCDelegate.h"
#import "TableCell.h"
@interface AllContactsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) id <VCDelegate>delegate;
@property (weak, nonatomic) IBOutlet UITableView *allContactsTableView;
@property (strong, nonatomic) NSMutableArray *allContactsArray;
@property (assign) int currentArrayIndexNumber;
- (IBAction)closeBtnTapped:(id)sender;
@end
如果您要连接文件所有者的插座,则应该执行不同的操作:
1 - 将 UITableViewCell 的类定义为您的 TableCell 类:
2 - 连接插座 不是从文件所有者,而是从对象列表中的 TableCell:
如果您使用的是 Xcode 6 GM,IB 中存在一个错误,可能会导致此错误。我不认为OP有这个问题,因为它更明显(你可以在日志中看到“未定义的类MyXXXCell”),但因为你像我一样从谷歌得到这个问题,只需尝试在IB中输入类名然后再次按 Enter 键,一旦它真正保存在 Storyboard/xml 文件中,就可以了。
我遇到了同样的问题,这里或其他讨论中的任何评论都帮助了我......最后我只是做了一个干净的原因,因为我很确定我做的一切都是正确的,是的......神奇地工作了!
使用 Xcode 12 也遇到同样的问题。只需在 Identity Inspector 上重新输入类即可解决问题。看起来像一个错误。
我在 Git 合并并解决了一些冲突之后就发生了这种情况。 它搞砸了我的 XCode 项目。
我尝试了整个 StackOverflow。起初我以为我的自定义类 .m 文件尚未添加到编译源类列表中。但这是一种情况,但是即使添加它之后,它也给了我同样的问题。
我是如何解决的:
干杯!
就我而言,我使用的是来自不同模块的笔尖,但笔尖在笔尖身份检查器的“自定义类”下列出了不正确的“模块”。它被检查为“从目标继承模块”,但我必须手动将 Nib 的模块设置为它所在的特定子模块。