为什么在UITableView中自定义单元格会收到错误:未收到initWithFrame:或initWithCoder?

希拉·阿夫沙尔(Hila Afshar)

我遵循了youtube上的教程,并且非常仔细地遵循了所有步骤三遍。但我收到此错误:

2014-09-27 04:02:42.248 JourneyTracker[6667:306214] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Requesting the window of a view 
(<CustomeCellTableViewCell: 0x8362f6f0; baseClass = UITableViewCell; frame = (0 0; 0 0); 
transform = [0, 0, 0, 0, 0, 0]; alpha = 0; opaque = NO; autoresize = W; layer = (null)>) with a nil layer. This view probably hasn't received initWithFrame: or initWithCoder:.'
            *** First throw call stack:
            (
                0   CoreFoundation                      0x00e0edf6 __exceptionPreprocess + 182

我搜索了许多相关问题,其中大多数都忘记了初始化按钮等。但是,正如您在下面看到的,我有两个标签并将它们初始化:

@implementation CustomeCellTableViewCell{

    UILabel *_nameValue;
    UILabel *_weightValue;

}

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self)
    {
        CGRect nameLableRect = CGRectMake(0, 5, 70, 15);
        UILabel *nameLable = [[UILabel alloc] initWithFrame:nameLableRect];
        nameLable.textAlignment = NSTextAlignmentRight;
        nameLable.text = @"name";
        nameLable.font = [UIFont boldSystemFontOfSize:12];
        [self.contentView addSubview:nameLable];


        CGRect weightLableRect = CGRectMake(0, 26, 70, 15);
        UILabel *weightLable = [[UILabel alloc] initWithFrame:weightLableRect];
        weightLable.textAlignment = NSTextAlignmentRight;
        weightLable.text = @"weight";
        weightLable.font = [UIFont boldSystemFontOfSize:12];
        [self.contentView addSubview:weightLable];

        //Add content
        CGRect nameValueRectangle = CGRectMake(80, 5, 200, 15);
        _nameValue = [[UILabel alloc] initWithFrame:nameValueRectangle];
        [self.contentView addSubview:_nameValue];


        CGRect weightValueRectangle = CGRectMake(80, 5, 200, 15);
        _weightValue = [[UILabel alloc] initWithFrame:weightValueRectangle];
        [self.contentView addSubview:_weightValue];

    }
    return self;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void) setName:(NSString *)n
{
    if (![n isEqualToString:_name])
    {
        _name = [n copy];
        _nameValue.text = _name;
    }
}

- (void) setWeight:(NSString *)w
{
    if (![w isEqualToString:_weight])
    {
        _weight = [w copy];
        _weightValue.text = _weight;
    }

}

@end

奇怪的是,当我在上面进行调试时awakeFromNib从未调用,并且在教程中我在youtube上关注时,他的代码中没有“ awakeFromNib”!

我已经逐行调试了此代码。首先UITableViewController进入(自定义类),然后进入自定义类(当它返回时UITableViewController将崩溃)。我在崩溃的代码中用<<< >>>>进行了注释。

这是我的UITableViewController

@implementation ListJourneyController

static NSString *cellIdentifier = @"CellTableIdentifier";


- (void)viewDidLoad {
    [super viewDidLoad];

 //Some implementation to modify array        

    UITableView *tableView = (id)[self.view viewWithTag:1];
    [tableView registerClass:[CustomeCellTableViewCell class] forCellReuseIdentifier:cellIdentifier];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section.
    return numberOfRow;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//<<<<<This point it goes to customize class and when it comes back it crashes>>>>>>
    CustomeCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    Journey *rowData = items[indexPath.row];
    cell.name = @"afshar";
    cell.weight = @"Hila";
    return cell;

}

更新解决此问题后,现在两个项目都出现在同一字段中!在此处输入图片说明

汤米·德沃伊(Tommy Devoy)

您永远不会super在CustomeCellTableViewCell类中调用实现。这就是为什么您会收到错误消息。将呼叫添加到super initWithStyle:reuseIdentifier,您将很方便。

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Now do your setup code
    }
    return self;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我会收到用于替换 python 数据框中的单元格内容的属性错误?

设置行单元格的文本时,为什么会收到“ System.ArgumentOutOfRangeException”?

为什么会收到错误消息,说明我的自定义日志记录处理程序“没有属性'level`”?

该视图可能尚未收到initWithFrame:或initWithCoder:

为什么自定义单元格为空白?

为什么会收到“构造函数未定义”错误?

为什么我不能在自定义单元格中为UIImageView创建@IBOutlet

为什么在尝试使用 openpyxl python 读取单元格值时收到“权限被拒绝错误”?

为什么会收到此NoReverseMatch错误?

为什么会收到ElementClickInterceptedException错误?

为什么会收到这些错误消息?

为什么在Xcode中的动作事件中总是会两次调用UITableView自定义单元格的第一个单元格

为什么使用自定义端口监视器在调用 EnumPorts 后会收到“数据无效”?

如果我尝试在 Template10-Validation 中自定义错误验证,我会收到 DependencyProperty 错误

自定义UITableView单元格动画

UITableView自定义单元格不显示数据

自定义单元格使UITableView跳转

如何正确配置UITableView自定义单元格

自定义单元格为空的UITableView

UITableView可重用的自定义单元格在子视图中显示错误的内容

UITableView + 2个自定义单元格=高度错误

如何使用 Swift 显示从 Viewcontroller B 接收到的值到 Viewcontroller A tableview 自定义单元格文本视图?

从核心数据中获取并使用自定义单元格显示在 UITableView 中

UITableView-自定义UITableViewCell中的自定义selectedBackgroundView选中后隐藏单元格分隔符

带有 Firebase 数据库的 UITableView 中的多个自定义单元格

自定义UITableView单元格中的UILabel不会随着核心数据的更改而更新

如何增加UITableView中的自定义单元格的宽度

带有自定义单元格的 UITableView 中的部分

UITableView中的两个自定义单元格