2011-05-14 62 views
0

我有一個UIView(稱爲:消息),與2 UILabel(標籤,dataLabel)和1 UIButton(iconButton)的單元格。iPhone在UITableViewController中的奇怪行爲

如果我釋放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;} 

謝謝!

回答

2

你不需要釋放iconButton作爲它的autorelease對象。

只有在分配對象或保留對象時,才應釋放對象。

我在發佈dataLabel時看不到任何問題。嘗試發佈dataLabel並跳過釋放iconButton,看看會發生什麼。

很可能它只是由於釋放iconButton,你不應該這樣做,因爲它是一個autorelease對象。