Ios 多次调用自定义UITableViewCell initWithStyle

Ios 多次调用自定义UITableViewCell initWithStyle,ios,objective-c,iphone,uitableview,ios7,Ios,Objective C,Iphone,Uitableview,Ios7,由于某些原因,在使用tableView CellForRowatineXpath方法时,自定义单元格被多次初始化。在我的自定义单元格中,我有一个自定义视图,可以使用AVPlayer播放AvItem。我的tableView cellForRowAtIndexPath方法如下所示 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

由于某些原因,在使用tableView CellForRowatineXpath方法时,自定义单元格被多次初始化。在我的自定义单元格中,我有一个自定义视图,可以使用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;
}
单元格多次初始化是否正常?我假设它会导致一些内存泄漏

两者

cell = [[MyCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

我们在做同样的事情

删除第一个并让
UITableView
执行其单元重用。它将为您调用init


此外,在iOS 6及更高版本中,有一个dequeueReusableCellWithReuseIdentifier:indexPath:方法,如果您提前注册了类/nib,该方法将始终返回一个单元格。很好

我认为这不是问题所在,但您应该只向tableView注册一次单元格/类
viewDidLoad
通常是一个不错的选择。感谢您的回答,但它仍被初始化多次。请确定,您的意思是屏幕上每个单元格不止一次,而不是一次,对吗?此外,如果您只使用
dequeueReusableCellWithIdentifier:
并删除对
initWithStyle:
cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];