为分组样式表创建自定义 UITableViewCell 时出现问题

发布于 2024-12-22 16:20:12 字数 2901 浏览 1 评论 0原文

我正在尝试创建一个自定义单元格以在分组样式 UITableView 中使用。但是,当我运行代码时,我总是在代码中标记的点处遇到错误。但我不知道出了什么问题。请问有人可以帮我吗?

.h 文件:

@interface CustomRecommendedStepCell : UITableViewCell
{
    UILabel *stepNumber;
    UITextView *stepInstruction;
}


@property(nonatomic, retain)IBOutlet UILabel *stepNumber;
@property(nonatomic, retain)IBOutlet UITextView *stepInstruction;


@end

.m 文件:

#import "CustomRecommendedStepCell.h"

@implementation CustomRecommendedStepCell
@synthesize stepNumber, stepInstruction;

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

    if (self)
    {
        UIImageView *backgroundView = [[UIImageView alloc] init];
        [backgroundView setImage:[UIImage imageNamed:@"Slate_bg.png"]];
        self.backgroundView = backgroundView;
        self.backgroundColor = [UIColor clearColor];

        stepNumber = [[UILabel alloc] init];
        stepNumber.textAlignment = UITextAlignmentLeft;
        stepNumber.font = [UIFont fontWithName:kCustomFont1 size:40];
        stepNumber.textColor =[UIColor whiteColor];
        stepNumber.backgroundColor = [UIColor clearColor];

        stepInstruction = [[UITextView alloc] init];
        stepInstruction.editable = NO;
        stepInstruction.textAlignment = UITextAlignmentLeft;
        stepInstruction.font = [UIFont fontWithName:kCustomFont1 size:23];
        stepInstruction.textColor =[UIColor whiteColor];
        stepInstruction.backgroundColor = [UIColor clearColor];

        [self.contentView addSubview:stepNumber];
        [self.contentView addSubview:stepInstruction];
    }

    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];

    stepNumber.frame = CGRectMake(20, 20, 50, 60);        // PROBLEM OCCURS HERE!
    stepInstruction.frame = CGRectMake(70, 20, 200, 60);
}


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

    // Configure the view for the selected state
}

@end

这是我收到的日志消息的副本:

2011-12-22 20:34:09.801 Some App[30945:10703] -[__NSCFString setFrame:]: unrecognized selector sent to instance 0x756b340
2011-12-22 20:34:09.802 Some App[30945:10703] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString setFrame:]: unrecognized selector sent to instance 0x756b340'
*** First throw call stack:
(0x11f052 0x1845d0a 0x120ced 0x85f00 0x85ce2 0xd7c1 0x778322 0x120e72 0x359792d 0x35a1827 0x35a1922 0x771f47 0x924edd 0x7d2fe1 0x7d3589 0x7bedfd 0x7cd851 0x778301 0x120e72 0x359792d 0x35a1827 0x3527fa7 0x3529ea6 0x3529580 0xf39ce 0x8a670 0x564f6 0x55db4 0x55ccb 0x3ab4879 0x3ab493e 0x739a9b 0x2128 0x2085 0x1)
terminate called throwing an exception

I'm trying to create a custom cell for use in a grouped style UITableView. However when I run my code I keep hitting an error at the point marked in my code. I have no idea what is wrong though. Please could someone help me out?

.h file:

@interface CustomRecommendedStepCell : UITableViewCell
{
    UILabel *stepNumber;
    UITextView *stepInstruction;
}


@property(nonatomic, retain)IBOutlet UILabel *stepNumber;
@property(nonatomic, retain)IBOutlet UITextView *stepInstruction;


@end

.m file:

#import "CustomRecommendedStepCell.h"

@implementation CustomRecommendedStepCell
@synthesize stepNumber, stepInstruction;

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

    if (self)
    {
        UIImageView *backgroundView = [[UIImageView alloc] init];
        [backgroundView setImage:[UIImage imageNamed:@"Slate_bg.png"]];
        self.backgroundView = backgroundView;
        self.backgroundColor = [UIColor clearColor];

        stepNumber = [[UILabel alloc] init];
        stepNumber.textAlignment = UITextAlignmentLeft;
        stepNumber.font = [UIFont fontWithName:kCustomFont1 size:40];
        stepNumber.textColor =[UIColor whiteColor];
        stepNumber.backgroundColor = [UIColor clearColor];

        stepInstruction = [[UITextView alloc] init];
        stepInstruction.editable = NO;
        stepInstruction.textAlignment = UITextAlignmentLeft;
        stepInstruction.font = [UIFont fontWithName:kCustomFont1 size:23];
        stepInstruction.textColor =[UIColor whiteColor];
        stepInstruction.backgroundColor = [UIColor clearColor];

        [self.contentView addSubview:stepNumber];
        [self.contentView addSubview:stepInstruction];
    }

    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];

    stepNumber.frame = CGRectMake(20, 20, 50, 60);        // PROBLEM OCCURS HERE!
    stepInstruction.frame = CGRectMake(70, 20, 200, 60);
}


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

    // Configure the view for the selected state
}

@end

Here is a copy of the log messages I'm receiving:

2011-12-22 20:34:09.801 Some App[30945:10703] -[__NSCFString setFrame:]: unrecognized selector sent to instance 0x756b340
2011-12-22 20:34:09.802 Some App[30945:10703] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString setFrame:]: unrecognized selector sent to instance 0x756b340'
*** First throw call stack:
(0x11f052 0x1845d0a 0x120ced 0x85f00 0x85ce2 0xd7c1 0x778322 0x120e72 0x359792d 0x35a1827 0x35a1922 0x771f47 0x924edd 0x7d2fe1 0x7d3589 0x7bedfd 0x7cd851 0x778301 0x120e72 0x359792d 0x35a1827 0x3527fa7 0x3529ea6 0x3529580 0xf39ce 0x8a670 0x564f6 0x55db4 0x55ccb 0x3ab4879 0x3ab493e 0x739a9b 0x2128 0x2085 0x1)
terminate called throwing an exception

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

又怨 2024-12-29 16:20:12

在您的 tableView:cellForRowAtIndexPath: 方法中,您似乎做了类似的事情

cell.stepNumber = @"text";

而不是

cell.stepNumber.text = @"text";

In your tableView:cellForRowAtIndexPath: method you seem to do something like

cell.stepNumber = @"text";

instead of

cell.stepNumber.text = @"text";
情场扛把子 2024-12-29 16:20:12

我不认为问题出在这段代码中。按照尤金的建议检查您的 tableView:cellForRowAtIndexPath: 方法。我认为您可能会返回 NSString 而不是 UITableViewCell

I don't think the problem is in this code.. check your tableView:cellForRowAtIndexPath: method as Eugene suggested. I think you might be returning a NSString instead of a UITableViewCell?

染柒℉ 2024-12-29 16:20:12

看起来在调用 layoutSubviews 时,您的 stepNumber 标签已经被释放(或从未分配)。

我的猜测是,您需要查看实例化自定义单元的方式。您确定您的 initWithStyle: 方法被调用了吗?

您可以发布视图控制器的 cellForRowAtIndexPath: 方法吗?

It looks like by the time layoutSubviews is called, your stepNumber label has already been released (or never allocated).

My guess would be that you need to look at the way you are instantiating your custom cell. Are you positive your initWithStyle: method is getting called?

Can you post your cellForRowAtIndexPath: method of your view controller?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文