2016-07-23 69 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 
} 
相關問題