Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableViewCell子类导致无法识别的选择器发送问题_Ios_Objective C_Uitableview - Fatal编程技术网

Ios UITableViewCell子类导致无法识别的选择器发送问题

Ios UITableViewCell子类导致无法识别的选择器发送问题,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在子类化UITableViewCell,它导致我的应用程序崩溃。。。我遇到的问题是我的细胞一直在自我控制,所以我在搜索后找到的答案是一个子类,但我遇到了一些问题。这是我的密码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *requestCell = @"requestCell";

我正在子类化UITableViewCell,它导致我的应用程序崩溃。。。我遇到的问题是我的细胞一直在自我控制,所以我在搜索后找到的答案是一个子类,但我遇到了一些问题。这是我的密码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *requestCell = @"requestCell";

    RequestCell *cell;

    switch (indexPath.section) {
        {case 0:
            cell = [tableView dequeueReusableCellWithIdentifier:requestCell forIndexPath:indexPath];
            if (cell == nil) {
                cell = [[RequestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:requestCell];
            }

            //APP CRASHES ON THIS LINE 
            cell.requestTitle.frame = CGRectMake(60, 5, self.view.frame.size.width - 70, 30);
            cell.detailTitle.frame = CGRectMake(60, 30, self.view.frame.size.width - 70, 25);

            Request *request = [[Request alloc] init];
            request = self.isNotFilledList[indexPath.row];
            cell.requestTitle.text = request.productName;
            cell.detailTitle.text = request.dateRequested;
            cell.detailTitle.font = [UIFont systemFontOfSize:11.0];

            NSString *imageURL = @"myurl";
            imageURL = [imageURL stringByAppendingString:request.productImageName];

            cell.requestImageButton.frame = CGRectMake(5, 5, 50, 50);
            [cell.requestImageButton sd_setBackgroundImageWithURL:[NSURL URLWithString:imageURL] forState:UIControlStateNormal];

            NSLog(@"request name %@", request.productName);
            return cell;
        break;}

        {case 1:
            cell = [tableView dequeueReusableCellWithIdentifier:requestCell forIndexPath:indexPath];
            if (cell == nil) {
                cell = [[RequestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:requestCell];
            }

            Request *request = [[Request alloc] init];
            request = [self.isFilledList objectAtIndex:indexPath.row];
            cell.textLabel.text = request.productName;
            return cell;
            break;}

        default:
            break;
    }

    return cell;
}
还有我的RequestCell类

@interface RequestCell : UITableViewCell

@property (strong, nonatomic) UILabel *requestTitle;
@property (strong, nonatomic) UILabel *detailTitle;
@property (strong, nonatomic) UIButton *requestImageButton;
和.m文件

#import "RequestCell.h"

@implementation RequestCell

- (void)awakeFromNib {
    // Initialization code
}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {


    }
    return self;
}

-(UILabel *)requestTitle
{
    if(!_requestTitle)
    {
        _requestTitle = [[UILabel alloc] init];
    }
    return _requestTitle;
}

-(UILabel *)detailTitle
{
    if(!_detailTitle)
    {
        _detailTitle = [[UILabel alloc] init];
    }
    return _detailTitle;
}


-(UIButton *)requestImageButton
{
    if(!_requestImageButton)
    {
        _requestImageButton = [[UIButton alloc] init];
    }
    return _requestImageButton;
}



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

    // Configure the view for the selected state
}
我做错了什么

原始代码**在顶部重新绘制单元格

switch (indexPath.section) {
        {case 0:
            cell = [tableView dequeueReusableCellWithIdentifier:requestCell forIndexPath:indexPath];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:requestCell];
            }

            UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(55, 5, self.view.frame.size.width - 60, 30)];
            UILabel *detailTitle = [[UILabel alloc] initWithFrame:CGRectMake(55, 30, self.view.frame.size.width - 60, 25)];

            Request *request = [[Request alloc] init];
            request = self.isNotFilledList[indexPath.row];
            title.text = request.productName;
            detailTitle.text = request.dateRequested;

            NSString *imageURL = @"myurl";
            imageURL = [imageURL stringByAppendingString:request.productImageName];

            UIButton *keyImageButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 50, 50)];
            //[[keyImageButton imageView] setContentMode: UIViewContentModeScaleAspectFill];
            [keyImageButton sd_setBackgroundImageWithURL:[NSURL URLWithString:imageURL] forState:UIControlStateNormal];

            [cell addSubview:detailTitle];
            [cell addSubview:title];
            [cell addSubview:keyImageButton];

            NSLog(@"request name %@", request.productName);
            return cell;
        break;}

您不需要手动实例化单元实例变量。尝试删除额外的函数

-(UILabel *)requestTitle
{
    if(!_requestTitle)
    {
        _requestTitle = [[UILabel alloc] init];
    }
    return _requestTitle;
}

-(UILabel *)detailTitle
{
    if(!_detailTitle)
    {
        _detailTitle = [[UILabel alloc] init];
    }
    return _detailTitle;
}


-(UIButton *)requestImageButton
{
    if(!_requestImageButton)
    {
        _requestImageButton = [[UIButton alloc] init];
    }
    return _requestImageButton;
}
赞成这个

@implementation RequestCell
@synethesize requestTitle;
@synthesize detailTitle;
@synthesize requestImageButton;
编辑:

啊,我想我现在看到问题了。您正在使用的是新的iOS 6版本的
dequeueReusableCellWithIdentifier:forIndexPath:
,它的设计目的是永远不会返回nil单元格(以防止应用程序崩溃和为开发人员带来便利)。因此,下面的if语句是针对旧的dequeue方法的,它只是
dequeueReusableCellWithIdentifier

修复程序应更改以下内容:

cell = [tableView dequeueReusableCellWithIdentifier:requestCell forIndexPath:indexPath];
为此:

cell = [tableView dequeueReusableCellWithIdentifier:requestCell];

如果您的单元格是在故事板中生成的,则调用的init方法是
initWithCoder:
,而不是
initWithStyle:reuseIdentifier:
。因此,覆盖它,创建标签,并将它们添加到contentView中。另外,在
cellforrowatinexpath
方法中没有理由使用if cell==nil子句,因为您的出列方法保证返回一个单元格

你应该改变的另一件事是

Request *request = [[Request alloc] init];
request = self.isNotFilledList[indexPath.row];
在第一行实例化一个请求对象,然后在下一行将其重新定义为self.isNotFilledList[indexPath.row]来丢弃它。你应该

Request *request = self.isNotFilledList[indexPath.row];

请发布您的确切错误。-[UITableViewCell requestTitle]:无法识别的选择器发送到实例0x13fe17b20
2015-01-02 23:28:49.982 Redrum[2729:1003782]***由于未捕获的异常“NSInvalidArgumentException”,终止应用程序,原因:'-[UITableViewCell requestTitle]:无法识别的选择器发送到实例0x13fe17b20'
我认为出于某种原因,您正在将UITableViewCell(没有requestTitle属性)而不是RequestCell出列。您的代码中碰巧有一行
registerClass
?我是否需要设置一个Xib文件以与我的
RequestCell
配合使用?我的故事板中有一个原型单元@“RequestCell”,当然有,因为每次重复使用一个单元时,您都会添加一个新标签。在单元格的类中重写initWithCoder,创建标签,并将它们添加到contentView中。现在删除所有其他代码。hmmm。这给了我同样的问题,使用新的出列方法不是问题。if cell==nil子句是无用的,但它不会造成任何伤害。它是有害的,因为它使代码看起来像是在实例化RequestCell,而实际上iOS正在返回UITableViewCell的实例,使该单元格出列的方法永远不会返回可能是感知问题的RequestCell类型的单元格,但真正的问题是,他可能没有将情节提要中单元格的类更改为自定义类。使用旧的出列方法无法解决这个问题。这是正确的。我忘了在我的故事板中添加我的类。。改变这个仍然会给我空白单元格