自定义UITableViewCell给了我:这个类不符合键值编码

25 iphone uitableview ios

首先,这次讨论没有解决我的问题.

自定义UITableViewCell子类:此类不是符合编码规范的键值

建立:

我有一个Person对象数组MainViewController.要显示在一个对象UITableViewAllContactsViewController.

问题:

使用默认值时,所有工作都按预期工作UITableViewCell.当我使用我的自定义时,TableCell我得到一个错误,指出该类不符合键值编码.

我连接我的任何三个之后会出现此错误outletsIB我的TableCell班.

注意:

TableCell有三个属性:a name&emaillabel + 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
Run Code Online (Sandbox Code Playgroud)

TableCell.m

#import "TableCell.h"

@implementation TableCell
@synthesize nameLabel, imageView, emailLabel;
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

更新:

也许截图可以帮助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
Run Code Online (Sandbox Code Playgroud)

Nat*_* R. 61

如果您要连接文件所有者的插座,您应该做不同的事情:

1 - 为您的TableCell类定义UITableViewCell的类:

在此输入图像描述

2 - 不是从文件所有者连接插座,而是从对象列表中的TableCell连接插座:

连接插座

  • 谢谢,连接TableCell的插座而不是File的所有者也为我解决了这个问题.是否有必要以这种方式完成的原因?实际上我犯了这个错误几次,所以我想知道为什么Xcode不会自动创建一个UITableViewCell子类和.xib,这样你就可以像在其他UI组件连接时那样连接自定义单元格中的新组件. Interface Builder? (2认同)

Rak*_*igo 6

我的项目中的一切都完成了,并且在尝试使用自定义单元及其属性时仍然收到了。这是我如何解决我的问题的。

CustomUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomUITableViewCell" forIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

真正让我震惊的是简单地CustomUITableViewCell从自定义单元格的“身份检查器”中删除该类并重新输入它..您将看到更改

  1. 模块值
  2. 从目标继承模块

让它们成为自动出现的值...不要尝试继承它或更改继承或模块值...

在此输入图像描述