我的应用程序中有UITableViewCell:
@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文件中连接到UILabel的IBOutlet ...
下面是我创建每个单元格的方式:
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是因为它是一个老项目。
发布于 2013-05-08 17:14:27
是的,每个保留的属性或实例变量都必须释放,IBOutlets也不例外。因为您使用属性,所以执行此操作的首选方法是:
-(void)dealloc {
self.name = nil;
self.views = nil;
//... and so on
[super dealloc];
}顺便说一句,你不需要像这样为你的属性声明“冗余的”实例变量:
IBOutlet UILabel *name;它在很久以前就需要了(在XCode 3时代),但现在编译器将为每个声明的属性自动生成它们。
发布于 2013-05-08 17:10:28
您可以使所有标签在自定义表格视图单元格分配,如果你需要它,保留和它,你必须释放在dealloc方法和分配nil在viewDidUnload.It避免内存泄漏。
发布于 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
https://stackoverflow.com/questions/16436527
复制相似问题