Ios 执行展开序列后未更新新数据
我对Swift比较陌生,在这个可能很容易解决的问题上磕磕绊绊,我就是想不出来,一直碰壁 我正在制作一个应用程序,其中一个用户点击集合视图控制器中的一个图像,然后它执行一个到主视图控制器的展开序列,并用用户选择的图像更新uiimageview。我让展开序列正常工作,但问题是uiimageview总是落后一个图像(即,我选择图像A,将序列向后展开,没有图像显示,因此我选择图像B将序列向后展开,然后我第一次选择的原始图像,图像A,在uiimageview中) 以下是目标视图控制器(我希望uiimageview根据集合视图中选择的图像进行更新的主视图控制器)的代码 在“集合视图”控制器上,用户在其中选择图像并将其展开,我只需按住ctrl键并将单元格拖动到情节提要上的“退出”,然后选择“展开到VC”序列 就我的收藏视图而言,这里是如何设置的,因为我怀疑它与它的didSelectItemAtIndexPath部分有关,但我不确定Ios 执行展开序列后未更新新数据,ios,swift,uiimageview,uicollectionview,unwind-segue,Ios,Swift,Uiimageview,Uicollectionview,Unwind Segue,我对Swift比较陌生,在这个可能很容易解决的问题上磕磕绊绊,我就是想不出来,一直碰壁 我正在制作一个应用程序,其中一个用户点击集合视图控制器中的一个图像,然后它执行一个到主视图控制器的展开序列,并用用户选择的图像更新uiimageview。我让展开序列正常工作,但问题是uiimageview总是落后一个图像(即,我选择图像A,将序列向后展开,没有图像显示,因此我选择图像B将序列向后展开,然后我第一次选择的原始图像,图像A,在uiimageview中) 以下是目标视图控制器(我希望uiimage
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return francoPhotos.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! FrancoCellCollectionViewCell
// sets each cell of collection view to be a uiimage view of francoPhotos
let image = UIImage(named: francoPhotos[indexPath.row])
cell.francoImageCell.image = image
return cell
}
// highlights cell when touched
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
selectedFranco = francoPhotos[indexPath.row]
let cell = collectionView.cellForItemAtIndexPath(indexPath)
cell!.layer.borderWidth = 3.0
cell!.layer.borderColor = UIColor.blueColor().CGColor
}
override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath)
cell!.layer.borderWidth = 0.0
cell!.layer.borderColor = UIColor.clearColor().CGColor
}
很抱歉,如果我发布了太多的代码,但这是我的第一篇文章,就像我说的,我是开发新手,所以我想最好是包含一些额外的代码,而不是没有需要的东西
非常感谢您的建议
非常感谢!!!:) 之所以发生这种情况,是因为触发segue后调用了
collectionView:didSelectItemAtIndexPath:
。所以,它总是落后一个
解决此问题的一种方法是使用performsguewithidentifier
以编程方式调用展开序列,而不是将其从单元格连接到出口。为此,请将退出顺序从viewController顶部的viewController图标(最左侧图标)连接到退出图标(最右侧图标)
在文档大纲视图中找到此序列,并在右侧的属性检查器中为其指定一个标识符,如
“myExitSegue”
。然后在didSelectItemAtIndexPath
中,最后要做的事情是调用performsguewithidentifier(“myExitSegue”,sender:self)
下面是我要做的:
删除源VC unwind action中的代码以获得一个空的unwind segue,例如(显示新图像的逻辑将放在集合VC中):
为您的“展开”序列指定一个字符串标识符,以便您可以通过编程方式而不是自动执行序列。在这里,我只是叫我的“放松”
然后,在francollectioncollectionviewcontroller
的内部放置一个属性以保存所选UIImage:
var selectedFranco: UIImage!
在didSelectItemAtIndexPath
中,将属性设置为所选图像,然后手动执行以下步骤:
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// ...
self.selectedFranco = francoPhotos[indexPath.row]
self.performSegueWithIdentifier("Unwind", sender: self)
}
然后,我将在您的FrancoCollectionCollectionViewController
中添加prepareforsgue
方法,以将源代码视图控制器的图像设置为所选图像:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Unwind" {
let sourceVC = segue.destinationViewController as! YourSourceViewController
sourceVC.dragImage.image = selectedImageView.image
}
}
不确定这是否有帮助,因为这是一个需要解决的重大问题。解决这个问题可能需要很多工作。但是在放松之后,在CollectionView上重新加载怎么样?因此,在ViewWillDisplay或ViewDidDisplay或viewdidlayoutsubviews或其他一些自定义方法中,您在collectionviewKate中调用重新加载,当您说您撞到墙时,您遇到的具体问题是什么?
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// ...
self.selectedFranco = francoPhotos[indexPath.row]
self.performSegueWithIdentifier("Unwind", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Unwind" {
let sourceVC = segue.destinationViewController as! YourSourceViewController
sourceVC.dragImage.image = selectedImageView.image
}
}