gpt4 book ai didi

swift - ReactiveCocoa-如何避免在 collectionView 的单元格中多次订阅信号

转载 作者:行者123 更新时间:2023-11-28 08:32:46 24 4
gpt4 key购买 nike

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时,这种方式又会被执行,Signal会被多次订阅,如何解决才能保证只订阅一次?

最佳答案

最后,我自己解决了...我使用“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
}

关于swift - ReactiveCocoa-如何避免在 collectionView 的单元格中多次订阅信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38542271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com