UITableViewCell不调用initWithFrame

时间:2011-07-31 18:00:17

标签: iphone objective-c ipad uitableview

我以编程方式创建了我的UITableViewCell(没有xib),这是我在代码中的方式:

- (id) initWithFrame: (CGRect)frame {
    self = [super initWithFrame: frame];
    if (self) {
        NSLog(@"CALLING INIT WITH FRAME");

        self.like_img = [[[UIImageView alloc] initWithFrame:CGRectMake(5, 75, 30, 30)] autorelease];
        [self.contentView addSubview: self.like_img];

        self.comment_img = [[[UIImageView alloc] initWithFrame:CGRectMake(5, 110, 30, 30)] autorelease];
        [self.contentView addSubview: self.comment_img];

        self.type_img = [[[UIImageView alloc] initWithFrame:CGRectMake(5, 40, 30, 30)] autorelease];
        [self.contentView addSubview:self.type_img];

        self.avatar = [[[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 30, 30)] autorelease];
        [self.contentView addSubview:self.avatar];

        self.post = [[[UITextView alloc] initWithFrame:CGRectMake(40, 40, 650, 100)] autorelease];
        [self.contentView addSubview:self.post];

        self.post_title= [[[UILabel alloc] initWithFrame:CGRectMake(40, 20, 650, 50)] autorelease];
        [self.contentView addSubview:self.post_title];

        self.post_detail = [[[UILabel alloc] initWithFrame:CGRectMake(40, 10, 650, 20)] autorelease];
        [self.contentView addSubview:self.post_detail];

    }

    NSLog(@"NOT MY SELF");
    return self;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"FTPostCell";

    FTPostCell *cell = (FTPostCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSLog(@"INITIALIZING CELL");
        cell = [[[FTPostCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

  //snip some code here
  return cell;
}

问题是我没有看到它调用initWithFrame被调用,为什么会这样? 因此,我所看到的只是空单......

1 个答案:

答案 0 :(得分:7)

因为您调用

而未调用initWithFrame

initWithFrame:reuseIdentifier:

替换

- (id) initWithFrame: (CGRect)frame {
    self = [super initWithFrame: frame];

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithFrame: frame reuseIdentifier:reuseIdentifier];

另外请注意,initWithFrame:reuseIdentifier是一个不推荐使用的方法,你应该使用initWithStyle:reuseIdentifier。

相关问题