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
}

データをリロードすると、この方法が再び実装され、Signal が何度もサブスクライブされます。

4

1 に答える 1

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
}
于 2016-07-23T22:39:28.377 に答える