UITableViewController中的iPhone奇怪行为

时间:2011-05-14 13:17:59

标签: iphone release uitableview

我有一个带有UIView(称为:message)的单元格,带有2个UILabel(label,dataLabel)和1个UIButton(iconButton)。

如果我发布dataLabel和iconButton,我在运行时收到错误。如果我释放其他人没有问题。

你能帮帮我吗?

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

static NSString *CellIdentifier = @"Cell";

UIImageView *balloonView;
UILabel *label;
UIButton *iconButton;
UILabel *dataLabel;


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellID"] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    UIView *message = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
    message.tag = 1;

    dataLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    dataLabel.backgroundColor = [UIColor clearColor];
    dataLabel.textColor = [UIColor darkGrayColor];
    dataLabel.tag = 2;
    dataLabel.numberOfLines = 0;
    dataLabel.lineBreakMode = UILineBreakModeWordWrap;
    dataLabel.font = [UIFont systemFontOfSize:11.0];
    dataLabel.textAlignment = UITextAlignmentCenter;
    dataLabel.opaque = YES;

    balloonView = [[UIImageView alloc] initWithFrame:CGRectZero];
    balloonView.tag = 3;
    balloonView.image = nil;

    label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.backgroundColor = [UIColor clearColor];
    label.tag = 4;
    label.numberOfLines = 0;
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.font = [UIFont systemFontOfSize:14.0];
    label.opaque = YES;

    iconButton = [UIButton buttonWithType:UIButtonTypeCustom];
    iconButton.tag = 5;

    [message addSubview:dataLabel];
    [message addSubview:balloonView];
    [message addSubview:label];
    [message addSubview:iconButton];

    [cell.contentView addSubview:message];

    [balloonView release];
    [label release];
    [message release];
}
else {
    dataLabel = (UILabel *)[[cell.contentView viewWithTag:0] viewWithTag:2];
    balloonView = (UIImageView *)[[cell.contentView viewWithTag:0] viewWithTag:3];
    label = (UILabel *)[[cell.contentView viewWithTag:0] viewWithTag:4];
    iconButton = (UIButton *)[[cell.contentView viewWithTag:0] viewWithTag:5];
}

dataLabel.frame = CGRectMake(0,0,cell.frame.size.width,20);
dataLabel.text = [NSString stringWithFormat:[[messages objectAtIndex:indexPath.row] valueForKey:DATE_TAG_NAME]];

UIImage *balloon;

[...]

balloonView.image = balloon;    

return cell;}

谢谢!

1 个答案:

答案 0 :(得分:2)

你不需要释放iconButton作为自动释放对象。

只有在分配对象或保留对象时才应该释放对象。

我在发布dataLabel时没有看到任何问题。尝试发布dataLabel并跳过释放iconButton并查看会发生什么。

最有可能的原因只是发布了你不应该做的iconButton,因为它是一个自动释放对象。