2016-07-23 66 views
1
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! ComposeCell 
    cell.deletePictureSignal.subscribeNext { (response) -> Void in 
     print("delete picture") 
     let deleteIndex = (self.pictureView.indexPathForCell(cell)?.item)! 
     self.pictures.removeAtIndex(deleteIndex) 
     //   self.pictureView.reloadData() 
    } 
    cell.addPictureSignal.subscribeNext { (response) -> Void in 
     print("add picture") 
     self.selectedIndex = (self.pictureView.indexPathForCell(cell)?.item)! 
     let imagePicker = UIImagePickerController() 
     imagePicker.delegate = self 
     self.presentViewController(imagePicker, animated: true, completion: nil) 
    } 
    return cell 
} 

当我reloadData,这样将再次实施,信号将被订阅很多时候,我怎么能解决这个问题,并确保订阅只有一次?ReactiveCocoa - 如何避免多认购信号的CollectionView的细胞

回答

0

最后,我用我自己解决吧... 我使用的“takeUntil”的方式,这样

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! ComposeCell 



    cell.deletePictureSigin.takeUntil(cell.rac_prepareForReuseSignal).subscribeNext { (response) -> Void in 
     print("delete Picture") 
     let deleteIndex:Int = (self.pictureView.indexPathForCell(cell)?.item)! 
     self.pictures.removeAtIndex(deleteIndex) 
     self.pictureView.reloadData() 
    } 

    cell.addPictureSigin.takeUntil(cell.rac_prepareForReuseSignal).subscribeNext { (response) -> Void in 
     print("add Picture") 
     self.selectedIndex = (self.pictureView.indexPathForCell(cell)?.item)! 
     let imagePicker = UIImagePickerController() 
     imagePicker.delegate = self 
     self.presentViewController(imagePicker, animated: true, completion: nil) 
    } 


    if indexPath.item == pictures.count { 
     cell.image = nil 
    } else { 
     cell.image = pictures[indexPath.item] 
    } 
    return cell 
} 
相关问题