iPhone 在 UITableViewController 中的奇怪行为

发布于 2024-11-07 09:21:54 字数 2671 浏览 0 评论 0原文

我有一个带有 UIView 的单元格(称为:消息),带有 2 个 UILabel(标签、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;}

谢谢你!

i have a cell with an UIView (called: message), with 2 UILabel (label, dataLabel) and 1 UIButton (iconButton).

If i release dataLabel and iconButton, i receive an error in runtime. If I release the others no problems occurred.

Can you please help me?

- (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;}

Thank you!

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

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

发布评论

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

评论(1

独闯女儿国 2024-11-14 09:21:54

您不需要释放 iconButton,因为它是自动释放对象。

您应该仅在分配或保留对象时释放对象。

我认为发布 dataLabel 没有任何问题。尝试释放 dataLabel 并跳过释放 iconButton,看看会发生什么。

最有可能的是,这只是由于释放 iconButton,你不应该这样做,因为它是一个自动释放对象。

You dont need to release iconButton as its an autorelease object.

You should release objects only either when you allocate it or retain it.

I dont see any problem in releasing dataLabel. Try releasing dataLabel and skip releasing iconButton and see what happens.

Most probably it was only due to releasing iconButton, which you shouldnt do, as its an autorelease object.

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