首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableViewCell dealloc方法

UITableViewCell dealloc方法
EN

Stack Overflow用户
提问于 2013-05-08 17:00:26
回答 5查看 3.3K关注 0票数 0

我的应用程序中有UITableViewCell

代码语言:javascript
复制
@interface ResultCell : UITableViewCell {
IBOutlet UILabel *name;
IBOutlet UILabel *views;
IBOutlet UILabel *time;
IBOutlet UILabel *rating;
IBOutlet UILabel *artist;

IBOutlet UIImageView *img;
}

@property (nonatomic, retain) UILabel *name;
@property (nonatomic, retain) UILabel *views;
@property (nonatomic, retain) UILabel *time;
@property (nonatomic, retain) UILabel *rating;
@property (nonatomic, retain) UILabel *artist;

@property (nonatomic, retain) UIImageView *img;

@end

并且所有这些在Xib文件中连接到UILabelIBOutlet ...

下面是我创建每个单元格的方式:

代码语言:javascript
复制
static NSString *CellIdentifier = @"ResultCell";
ResultCell *cell = (ResultCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil){
    UIViewController *vc = [[[UIViewController alloc] initWithNibName:@"ResultCell" bundle:nil] autorelease];
        cell = (ResultCell *) vc.view;
}

cell.name.text = item.name;
cell.views.text = item.viewCount;
cell.rating.text = [NSString stringWithFormat:@"%d%%",item.rating];
cell.time.text = item.timeStr;
cell.artist.text = item.artist;

我想知道在ResultCell类中,我是否需要实现一个dealoc方法并释放UILabel?还是像我做的那样没问题?我使用非ARC是因为它是一个老项目。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-05-08 17:14:27

是的,每个保留的属性或实例变量都必须释放,IBOutlets也不例外。因为您使用属性,所以执行此操作的首选方法是:

代码语言:javascript
复制
-(void)dealloc {
    self.name = nil;
    self.views = nil;
    //... and so on
    [super dealloc];
}

顺便说一句,你不需要像这样为你的属性声明“冗余的”实例变量:

代码语言:javascript
复制
IBOutlet UILabel *name;

它在很久以前就需要了(在XCode 3时代),但现在编译器将为每个声明的属性自动生成它们。

票数 2
EN

Stack Overflow用户

发布于 2013-05-08 17:10:28

您可以使所有标签在自定义表格视图单元格分配,如果你需要它,保留和它,你必须释放在dealloc方法和分配nil在viewDidUnload.It避免内存泄漏。

票数 0
EN

Stack Overflow用户

发布于 2013-05-08 17:14:07

是的,您必须在ResultCell类中编写dealloc方法来释放您合成的对象,以避免内存泄漏。有关更多了解,请参阅链接http://www.raywenderlich.com/4723/how-to-make-an-interface-with-horizontal-tables-like-the-pulse-news-app-part-2

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16436527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档