【问题标题】:ios: UITableViewCell crash with custom cell in storyboardios:UITableViewCell 在情节提要中使用自定义单元格崩溃
【发布时间】:2012-11-01 18:46:18
【问题描述】:

我有一个包含两个自定义单元格的表格,在尝试出列时会崩溃。

代码如下:

int row = indexPath.row;

static NSString *CellIdentifier;
UITableViewCell* cell;
PoetCell * poetCell;
PoemsCell * poemsCell;

if(row == 0) {

    CellIdentifier = @"PoetCell";

} else {

    CellIdentifier = @"poemsCell";        
}

if(row == 0) {

    poetCell= [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    poetCell.image.image=[UIImage imageWithData:imageData];

    if([imageData length]==0){

        poetCell.image.hidden=TRUE;
        poetCell.Name.frame=CGRectMake(124, poetCell.Name.frame.origin.y, poetCell.Name.frame.size.width, poetCell.Name.frame.size.height);

    } else {

        poetCell.Name.frame=CGRectMake(38, poetCell.Name.frame.origin.y, poetCell.Name.frame.size.width, poetCell.Name.frame.size.height);

    }

    poetCell.desc.text=pdesc;
    poetCell.Name.text=pName;

} else {

        poemsCell= [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if([poemsNames count]) {
            poemsCell.poemText.text=[poemsNames objectAtIndex:row-1];
        }
}

它在行上给出错误:

poetCell= [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];

[UITableViewCell setValue:forUndefinedKey:]:这个类不符合键名的键值编码。

我知道当笔尖中的链接错误时会出现此错误。但我认为代码中并非如此。所以任何人都可以了解问题所在吗?

编辑

我认为整个问题在于初始化poetCell和poetsCell,这就是代码

#import <UIKit/UIKit.h>

@interface PoetCell : UITableViewCell
{
    IBOutlet UILabel*Name;
    IBOutlet UILabel*desc;
    IBOutlet UIImageView*image;
}
@property(nonatomic,retain)IBOutlet UILabel*Name;
@property(nonatomic,retain)IBOutlet UILabel*desc;
@property(nonatomic,retain)IBOutlet UIImageView*image;

@end

#import "PoetCell.h"

@implementation PoetCell
@synthesize Name,desc,image;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

另一个单元格是一样的

【问题讨论】:

  • 请检查这是否使用不带 cellIdentifier 变量的单元格字符串。

标签: ios uitableview


【解决方案1】:

假设您的目标是 iOS5+,您是否在 viewDidLoad 中包含以下内容:

[self.tableView registerNib:[UINib nibWithNibName:kStandardCellIdentifier bundle:nil] forCellReuseIdentifier:(NSString *)]

【讨论】:

    【解决方案2】:

    您正在使用poetCell.Name。您能否确保您的自定义单元格具有“名称”属性(在您的自定义单元格的 .h/.m 中)?我想问题出在这里。

    顺便说一句,如果您的自定义单元格确实具有此属性,则应将其更改为 name 而不是 Name。以大写字母开头的变量用于类名。变量应始终以小写字母开头。

    您也应该发布自定义单元格的代码。


    编辑:

    如此处所示:Class is not key value coding-compliant

    在界面构建器中,当您应该从单元格视图链接它们时,您从文件所有者链接了您的 IBOutlets。

    你能检查一下吗?

    【讨论】:

    • 您能给我们您的自定义单元格的代码吗?你怎么能确定它来自 poetCell= [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 行?你能在下一行下一个断点,看看你是否到达它?
    • 大声笑,只是检查:) 如果没有“死”链接,你能检查你的笔尖吗?就像您可能已链接然后删除的旧标签一样?我不明白为什么您会在这一行中收到此错误..
    • 您是否将表格视图单元格的类设置为您在 IB 中的自定义类?
    • 你检查了“死”链接?
    • 我在情节提要中制作了一个全新的 viewController,并且没有连接任何 iboutlet,由于未捕获的异常 'NSInvalidArgumentException',它给了终止应用程序,原因:'-[UITableViewCell Name]:无法识别的选择器发送到实例0x9ce7280'
    【解决方案3】:

    您的代码可能有问题!是不是数组没有请求的索引?

    像这样进行检查

        if([poemsNames count] > row  ){
            poemsCell.poemText.text=[poemsNames objectAtIndex:row-1];
        } 
    

    【讨论】:

      猜你喜欢
      • 2014-04-01
      • 2014-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 2019-04-15
      • 2014-09-13
      相关资源
      最近更新 更多