Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 执行setCollectionViewLayout时,UICollectionViewCel中的布局看起来很奇怪_Ios_Objective C_Swift - Fatal编程技术网

Ios 执行setCollectionViewLayout时,UICollectionViewCel中的布局看起来很奇怪

Ios 执行setCollectionViewLayout时,UICollectionViewCel中的布局看起来很奇怪,ios,objective-c,swift,Ios,Objective C,Swift,我有两个nib文件,分别是RowRecordLayout和ColumnLayout。 我将nib单元格注册到UICollectionView,以便稍后在cellForItemAtIndexPath函数中使用。这是我的密码 override func viewDidLoad() { self.DJCollectionView.registerNib(UINib(nibName: "RowTemplateCell", bundle: NSBundle.mainBundle()), forCel

我有两个nib文件,分别是RowRecordLayout和ColumnLayout。

我将nib单元格注册到UICollectionView,以便稍后在cellForItemAtIndexPath函数中使用。这是我的密码

 override func viewDidLoad() {
 self.DJCollectionView.registerNib(UINib(nibName: "RowTemplateCell", bundle: NSBundle.mainBundle()), forCellWithReuseIdentifier: "DJCELL")
        self.DJCollectionView.registerNib(UINib(nibName: "ColumnTemplateCell", bundle: NSBundle.mainBundle()), forCellWithReuseIdentifier: "DJCELL2")
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath
    indexPath: NSIndexPath) -> UICollectionViewCell {

  var ProductCardCellpt : DJCell?

    if self._CurrentLayoutType == enum_Layout.RowRecords {
    reuseIdentifier = "DJCELL"
    ProductCardCellpt = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as? DJCell
    …
    }else{
    reuseIdentifier = "DJCELL2"
    ProductCardCellpt = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as? DJCell
    ...
    }
    return   ProductCardCellpt!
    }
最后当我按下DJ按钮。除了单元内的意外约束外,过渡之间的动画正常

这是过渡布局的代码

 @IBAction func Action_ToggleLayout(sender: UIBarButtonItem) {
        if _CurrentLayoutType == enum_Layout.TwoColumn {
            _CurrentLayoutType = enum_Layout.RowRecords
            self.DJCollectionView.setCollectionViewLayout(self._RowRecordsLayout_CollectionViewLayout!, animated: true, completion: { (IsDone) -> Void in

            })
        }
        else{
            _CurrentLayoutType = enum_Layout.TwoColumn
            self.DJCollectionView.setCollectionViewLayout(self._TwoColumnLayout_CollectionViewLayout!, animated: true, completion: { (IsDone) -> Void in

                }
            })
        }

    }
当我向上或向下滚动时,单元格内的内容恢复正常。
有什么解决办法吗?

问题是当前显示的单元格在更改布局时不会更新。它们仅在您滚动时更新。当布局发生更改时,执行类似于调用collectionView.reloadData()的操作应该可以完成这项工作

但我认为您没有正确使用collectionView。布局与UICollectionViewCell无关,动画将无法工作。因此,我认为在更改UICollectionView布局时更改collectionView单元格类不是一个好的做法。您应该重写自定义collectionViewCell的方法willTransitionFromLayout:,并在此处更改单元格的约束。差不多


希望这能引导你走上正确的道路。

在我做了很多实验之后。我确实结合了Poql的答案来实现这个结果。程序如下

  • 在未连接nib的情况下,将单元格类注册到UICollectionView 在自定义单元类中使用initwithframe,因为没有nib文件,也没有脚本引用单元
  • 在单元格类内声明用于切换布局实现的函数
  • 重写函数将从单元格类内的布局转换
  • 重写单元格类内的函数applyLayoutAttributes
  • 最后,在collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)函数中的collectionView.dequeueReusableCellWithReuseIdentifier之后调用切换函数
  • 下面是代码的一部分

     func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
         var ProductCardCellpt : ProductCardCell? = collectionView.dequeueReusableCellWithReuseIdentifier("ProductCardCell", forIndexPath: indexPath) as? ProductCardCell
         ProductCardCellpt?.SetLayoutAccordingToDesiredLayout(self._CurrentLayoutType)
     }
    
    在细胞的类内

    func SetLayoutAccordingToDesiredLayout(DesiredLayout : enum_Layout){
         if DesiredLayout == self.Current_Layout && IsSetPosition == true {
            return
         }
         self.IsSetPosition = false
         self.Current_Layout = DesiredLayout
         if DesiredLayout == enum_Layout.OneColumn || DesiredLayout == enum_Layout.TwoColumn {
         DoColumnLayout()
         }else{
         DoRecordLayout()
         }
     }
    func DoRecordLayout(){
        let dFrame = self.contentView.frame
        self.Thumbnail.frame = CGRect(x: 0, y: 0, width: dFrame.height, height: dFrame.height)
        ...
     }
    func DoColumnLayout(){
        let dFrame = self.contentView.frame
        self.Thumbnail.frame = CGRect(x: 0, y: 0, width: dFrame.width, height: dFrame.width)
        ...
    }
    override func applyLayoutAttributes(layoutAttributes: UICollectionViewLayoutAttributes!) {
        self.layoutIfNeeded()
        self.SetLayoutAccordingToDesiredLayout(self.Current_Layout)
    
    }
    override func willTransitionFromLayout(oldLayout: UICollectionViewLayout, toLayout newLayout: UICollectionViewLayout) {
        let nLayout = newLayout as! ProductCollectionViewLayout
        let enumLayout = nLayout._enumLayout
        self.Current_Layout = enumLayout
    }
    

    您可以将collectionView布局放在nib文件中吗?您只能将UICollectionViewCell放在nib中,不是吗?它们是nib中的UICollectionViewCell。您何时更改了collectionView的布局?触摸DJ按钮时,布局会更改。我已经编辑了我的文章,包括更改布局代码。