我的tableview会创建重复的子视图。我搜索了很多,但仍然无法解决这个问题

时间:2014-06-29 18:25:20

标签: ios objective-c uitableview

摘要 我想删除自定义表格视图单元格中的重复子视图。滚动桌面视图后,我的自定义表格视图单元格中的旧视频播放器视图和按钮始终显示。我想要删除旧的视频视图,并且按钮一次又一次出现。

我做了什么

StoryImage

  1. 我在我的项目中使用了storyboard并制作了自定义的tableview单元格来填充整个屏幕尺寸。 !
  2. 我设置了self.tableview.pagingEnabled = YES。
  3. 我在这段代码中使用了ReactiveCocoa,Parse.com和SCRecorder(用于播放像Vine这样的视频)。
  4. 我在我的自定义tableviewCell类(称为MainViewCell)中为自定义UITableViewCell中的每个子视图创建了IBOutlet。使用此自定义tableviewCell的tableview控制器称为MainViewController。
  5. 我的问题

    确定。我承认这应该是一个非常容易的问题。但是我无法解决这个问题,尽管到目前为止我通过google和堆栈溢出搜索并应用了所有方法。我的tableview单元格一遍又一遍地创建子视图。

    我的假设

    首先,我创建另一个videoPlayerview的主要假设是重用单元格在重用时会反复创建另一个回放视图。

    1. 重用单元格将再次创建另一个SCVideoPlayerView(VideoPlayerView)。
    2. 所以,我试图在prepareForReuse中将videoPlayerview设为nil,就像这样

      -(void)prepareForReuse
      {
          [super prepareForReuse];
          self.buyButton = nil;
          self.moreButton = nil;
          self.messageButton =nil;
          self.usernameButton = nil;
          self.videoPlayerView = nil;
      }
      

      当我这样做时,会发生更奇怪的事情。存在基础视图,删除了新视图,这不是我想要的。

      当我在(MainViewCell *)tableView :( UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中单元格不为nil时删除videoPlayerview, 当我这样做时,视图本身被完全删除。我也尝试了这个(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell1 forRowAtIndexPath:(NSIndexPath *)indexPath。但是,它表现出相同的行为。

      我还尝试使用removeFromSuperLayer方法删除prepareForReuse中SCVideoPlayerView类中的(强,非原子,读写)AVPlayerLayer * playerLayer子图层。当我这样做时,它崩溃了我的应用程序。

      对于按钮,我在prepareForReuse方法中将按钮设置为nil。但它也删除了新视图,而不是旧视图。以下是此问题的完整代码段。

      MainViewController

      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
        static NSString *CellIdentifier = @"MainCell";
        MainViewCell *cell = (MainViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
      
      
      self.cell = cell;
      PFObject *product = [self.products objectAtIndex:indexPath.row];
      self.product = product;
      self.title = [product objectForKey:@"productName"];
      PFUser *uploader = [self.owners objectAtIndex:indexPath.row];
      self.uploader = uploader;
      [cell configureCellWithProduct:product andUploader:uploader withIndexPath:indexPath];
      
      [[[cell.usernameButton rac_signalForControlEvents:UIControlEventTouchUpInside]doNext:^(id x) {
          cell.usernameButton.enabled = NO;
      }] subscribeNext:^(id x) {
          cell.usernameButton.enabled = YES;
          [self stopVideo];
          [self performSegueWithIdentifier:@"myPine" sender:self];
      }];
      
      [[[cell.buyButton rac_signalForControlEvents:UIControlEventTouchUpInside]doNext:^(id x) {
          cell.buyButton.enabled = NO;
      }] subscribeNext:^(id x) {
          cell.buyButton.enabled = YES;
          [self stopVideo];
          [self performSegueWithIdentifier:@"buyThisOne" sender:self];
      }];
      
      [[[cell.messageButton rac_signalForControlEvents:UIControlEventTouchUpInside]doNext:^(id x) {
          cell.messageButton.enabled = NO;
      }] subscribeNext:^(id x) {
          cell.messageButton.enabled = YES;
          [self stopVideo];
          [self performSegueWithIdentifier:@"showMessages" sender:self];
      }];
      
      [[[cell.moreButton rac_signalForControlEvents:UIControlEventTouchUpInside] doNext:^(id x) {
          cell.moreButton.enabled = NO;
      }] subscribeNext:^(id x) {
          cell.moreButton.enabled = YES;
          [self stopVideo];
          [self showActionSheet];
      }];
      
      
      return cell;
      

      }

      MainViewCell.h

      @interface MainViewCell : UITableViewCell
      {
          SCPlayer *player;
      }
      @property (weak, nonatomic) IBOutlet SCVideoPlayerView *videoPlayerView;
      @property (weak, nonatomic) IBOutlet UIImageView *profileImage;
      @property (weak, nonatomic) IBOutlet UIButton *usernameButton;
      @property (weak, nonatomic) IBOutlet UILabel *priceLabel;
      @property (weak, nonatomic) IBOutlet UILabel *locationLabel;
      @property (weak, nonatomic) IBOutlet UILabel *yearLabel;
      @property (weak, nonatomic) IBOutlet UIButton *buyButton;
      @property (weak, nonatomic) IBOutlet UIButton *messageButton;
      @property (weak, nonatomic) IBOutlet UIButton *moreButton;
      
      
      
      -(void)configureCellWithProduct:(PFObject *)product andUploader:(PFUser *)uploader withIndexPath:(NSIndexPath *)indexPath;
      

      MainViewCell.m

      -(void)configureCellWithProduct:(PFObject *)product andUploader:(PFUser *)uploader withIndexPath:(NSIndexPath *)indexPath
      {
      
      NSLog(@"Configure cell is called");
      CacheManagement *cacheManagement = [CacheManagement sharedStore];
      self.uploader = uploader;
      self.product = product;
      NSNumber *price = [product objectForKey:@"productPrice"];
      [self.usernameButton setTitle:[uploader objectForKey:@"name"] forState:UIControlStateNormal];
      [self.usernameButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
      self.priceLabel.text = [NSString stringWithFormat:@"$%@", price.stringValue];
      self.yearLabel.text = [product objectForKey:@"productYear"];
      self.locationLabel.text = [uploader objectForKey:@"currentCity"];
      
      
      __block PFFile *videoFile =[product objectForKey:@"videoFile"];
      
      EGOCache *cache = [EGOCache globalCache];
      
      dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
      dispatch_async(queue, ^{
      
          NSString *fileName = [NSString stringWithFormat:@"Video%ld", [CacheManagement sharedStore].productCount- indexPath.row];
          NSString *key = [fileName stringByAppendingPathExtension:@".mov"];
      
          if(![cache hasCacheForKey:key])
          {
              [cacheManagement videoDownload:videoFile withIndexPath:indexPath forVideoView:self.videoPlayerView];
      
          }
          else
          {
              [self playVideoWithKey:key];
          }
      
      });
      
      
      
      if([cache hasCacheForKey:self.product.objectId])
      {
          NSData *imageData = [cache dataForKey:@"images"];
          self.avatars = [NSKeyedUnarchiver unarchiveObjectWithData:imageData];
          NSLog(@"self.avatars : %@", self.avatars);
          NSLog(@"We have cache for uploader!");
          UIImage *profileImage = [self.avatars objectForKey:[self.uploader objectForKey:@"name"]];
          dispatch_async(dispatch_get_main_queue(), ^{
              self.profileImage.image = profileImage;
          });
      }
      else
      {
          ImageUtil *downloader = [ImageUtil sharedStore];
          [downloader profileImageDownloadforUser:self.uploader completion:^(UIImage *image) {
              [cacheManagement saveCacheForProfileImage:image withObjectId:self.uploader.objectId];
              dispatch_async(dispatch_get_main_queue(), ^{
                  self.profileImage.image = image;
              });
          }];
      
      }
      }
      
      -(void)awakeFromNib
      {
      [super awakeFromNib];
      NSLog(@"Awake from nib!");
      player = self.videoPlayerView.player;
      player.delegate = self.videoPlayerView;
      UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
      gestureRecognizer.numberOfTapsRequired=1;
      [self.videoPlayerView addGestureRecognizer:gestureRecognizer];
      
      if([player isPlaying])
      {
          [self stopVideo];
      }
      }
      
      
      -(void)playVideoWithKey:(NSString *)key
      {
      EGOCache *cache = [EGOCache globalCache];
      if(self.videoPlayerView.player !=nil)
      {
          NSLog(@"Reinitializing video PlayerView");
          SCVideoPlayerView *playerView = [self.videoPlayerView initWithPlayer:player];
          playerView.frame = self.videoPlayerView.frame;
          self.videoPlayerView= nil;
      
          self.videoPlayerView = playerView;
      }
      [self setVideoPath:[cache cachePathForTheKey:key]];
      dispatch_async(dispatch_get_main_queue(), ^{
      
          NSURL *url = [NSURL fileURLWithPath:self.videoPath];
          [player setItemByUrl:url];
      
          player.shouldLoop = YES;
      
      
          NSLog(@"Playing video");
          [player play];
      });
      

      }

      -(void)prepareForReuse
      {
         [super prepareForReuse];
         self.buyButton = nil;
         self.moreButton = nil;
         self.messageButton =nil;
         self.usernameButton = nil;
      }
      

0 个答案:

没有答案