【问题标题】:iPhone: how to dealloc custom cell labelsiPhone:如何释放自定义单元格标签
【发布时间】:2011-06-20 00:06:17
【问题描述】:

嗨,我正在为 twitter 开发 UITableView 自定义单元格,对于每个单元格,我正在添加 UIImageView(userPhoto)、UILabel(name)、UILabel(tweet)、UILabel(Data),我必须释放所有元素,

请浏览附件中的 INSTRUMENT TOOL alloc 截图

当我运行 Instrument 工具时,它显示了一些不是 dealloc,如何dealloc, 这是我的代码

//自定义表格视图单元格的外观。 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 static NSString *identifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];


if(!cell) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewStyleGrouped reuseIdentifier:nil];

    //cell.selectionStyle = UITableViewCellSelectionStyleNone;

UILabel *name  = [[UILabel alloc]initWithFrame:CGRectMake(72,3,242,15)] ;
name.text   = (NSString*)[(Tweet*)[tweetArray objectAtIndex:indexPath.row] userName];
[name setFont:[UIFont fontWithName:@"Helvetica" size:13]];
name.textColor=[UIColor colorWithRed:250 green:250 blue:210 alpha:0.5];
[name setBackgroundColor:[UIColor clearColor]];



UILabel *date  = [[[UILabel alloc]initWithFrame:CGRectMake(200,3,130,15)] autorelease];
date.text   = (NSString*)[(Tweet*)[tweetArray objectAtIndex:indexPath.row] created_at];
[date setFont:[UIFont fontWithName:@"Helvetica" size:12]];
date.textColor=[UIColor colorWithRed:250 green:250 blue:210 alpha:0.5];
[date setBackgroundColor:[UIColor clearColor]];



UILabel * tweetLabel  = [[UILabel alloc]initWithFrame:CGRectMake(72,20,242,60)] ;
tweetLabel.text   = (NSString*)[(Tweet*)[tweetArray objectAtIndex:indexPath.row] tweet];
tweetLabel.numberOfLines = 3;
tweetLabel.textColor=[UIColor colorWithRed:252 green:148 blue:31 alpha:1];
[tweetLabel setFont:[UIFont fontWithName:@"Helvetica" size:12]];
[tweetLabel setBackgroundColor:[UIColor clearColor]];

UIImageView *myImage = [[UIImageView alloc]initWithFrame:CGRectMake(6,10,58,60)] ;
NSURL *url = [NSURL URLWithString:[(Tweet*)[tweetArray objectAtIndex:indexPath.row] image_url]];
NSData *data = [NSData dataWithContentsOfURL:url];
[myImage setImage: [UIImage imageWithData:data]];

[cell.contentView addSubview:myImage];
[cell.contentView addSubview:tweetLabel];
[cell.contentView addSubview:name];
[cell.contentView addSubview:date];

[myTweetActivityIndi​​cator 停止动画]; [myTweetActivityIndi​​cator setHidesWhenStopped:YES];

return cell;

}

请指导我 谢谢

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

    你可以使用

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewStyleGrouped reuseIdentifier:nil] autorelease];
    

    对于其他标签,你大概可以使用

     [date release]; [tweetLabel release]; [name release]; [myImage release];
    

    【讨论】:

    • 嗨,Harinder,所有这些 UIImage 和 UILabel 都已发布,我确实自动发布了,它在 NSUrl 中显示了一些泄漏,[myImage setImage: [UIImage imageWithData:data]]; //这条线很多,这个,,,,即使我也发布了UIImage和UILabel,它显示没有发生错位,请告诉我谢谢
    【解决方案2】:

    您必须在完成使用init/alloc(名称、日期、tweetLabel、myImage)分配的所有对象(例如例如,在addSubview 之后)。注意,根据documentation,视图由接收方保留。

    【讨论】:

    • 谢谢艾琳,我是在代码中完成的,但我在给你的帖子中错过了它,我遇到了麻烦,就像这行 NSData *data = [NSData dataWithContentsOfURL:url]; [myImage setImage: [UIImage imageWithData:data]];谢谢,请检查并告诉,如何释放这个 NSData 对象数据,如果我正在释放那个 userPhoto 没有来,如果不是它不是释放如何处理这个
    【解决方案3】:

    尝试使用

    NSData *data = [[NSData alloc] initWithContentsOfURL:url];
    

    [myImage setImage: [UIImage imageWithData:data]]; 【数据发布】; 数据 = 无;

    在这种情况下,我们手动释放分配的内存。我希望,这行得通

    【讨论】:

      猜你喜欢
      • 2011-02-13
      • 1970-01-01
      • 2010-11-18
      • 2013-06-07
      • 2015-05-31
      • 2017-03-14
      • 2020-02-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多