【问题标题】:Issue creating custom UITableViewCell for a grouped style table为分组样式表创建自定义 UITableViewCell 的问题
【发布时间】:2011-12-22 20:35:52
【问题描述】:

我正在尝试创建一个自定义单元格以用于分组样式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 一些应用程序[30945:10703]-[__NSCFString setFrame:]:无法识别的选择器发送到实例 0x756b340 2011-12-22 20:34:09.802 某些应用程序 [30945:10703] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFString setFrame:]:无法识别的选择器发送到实例 0x756b340” *** 首先抛出调用堆栈: (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) 终止称为抛出异常

【问题讨论】:

  • 你的对象看起来像NSString

标签: iphone objective-c uitableview cgrectmake


【解决方案1】:

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

cell.stepNumber = @"text";

而不是

cell.stepNumber.text = @"text";

【讨论】:

    【解决方案2】:

    我不认为问题出在此代码中.. 请按照 Eugene 的建议检查您的 tableView:cellForRowAtIndexPath: 方法。我想你可能会返回 NSString 而不是 UITableViewCell

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

      • 日志中提到NSCFString 无法识别setFrame: 选择器。
      • 这并不意味着什么,ivar可能只是指向NSCFString之前使用的垃圾内存......
      猜你喜欢
      • 2021-11-10
      • 1970-01-01
      • 2013-08-29
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 2012-08-08
      • 1970-01-01
      • 2013-02-26
      相关资源
      最近更新 更多