简体   繁体   中英

ReactiveCocoa-How can i avoid to multiple subscribe the signal in collectionView's cell

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
}

when I reloadData,this way will be implemented again,and Signal will be subscribe many time,how can I resolve it and ensure subscribe only once?

finally,I resolve it by my self... I use the way of "takeUntil",like this

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM