【发布时间】:2014-08-16 05:17:32
【问题描述】:
由于某种原因,我的自定义单元格在使用 tableView cellForRowAtIndexPath 方法时被多次初始化。在我的自定义单元格中,我有一个自定义视图来使用 AVPlayer 播放 AvItem。我的 tableView cellForRowAtIndexPath 方法看起来像这样
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell==nil) {
[tableView registerClass:[MyCustomCell class] forCellReuseIdentifier:@"cell"];
cell = [[MyCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
}
MyCoreDataVideos *videos = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSURL * videoLocalURL = [[MyModel sharedInstance]localVideoDestinationFromVideoPath:videos.videoPath];
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:videoLocalURL];
[cell.videoView.player setItem:item];
[cell.videoView.player play];
return cell;
}
而我的自定义单元格的 initWithStyle 方法如下所示
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
NSLog(@"I'm being called a lot of times ");
_videoView = [[MyVideoPlayerView alloc]init];
_videoView.frame = self.contentView.bounds;
[self.videoView.player setShouldLoop:YES];
[self.videoView setTapActionsEnabled:YES];
self.contentView.clipsToBounds = YES;
[self.contentView addSubview:_videoView];
[self layoutSubviews];
}
return self;
}
单元被多次初始化是否正常?我假设它会导致一些内存泄漏。
【问题讨论】:
-
我认为这不是问题,但您应该只向 tableView 注册一次单元格/类。
viewDidLoad通常是个好地方。
标签: ios objective-c iphone uitableview ios7