【问题标题】:CollectionView objects (Swift)集合视图对象 (Swift)
【发布时间】:2015-04-24 22:05:52
【问题描述】:

我希望高亮显示更改集合视图中对象的大小和外观。

如何在“didHighlight”方法中在集合视图单元格中设置对象属性?

在“cellForItemAtIndexPath”中,您将可重复使用的单元格声明为类

只需使用“cell.MyOutlet.backgroundColor = UIColor.blueColor()”

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        if collectionView == self.CollectionViewController {
            let (FriendFirstName,FriendLastName) = friends[indexPath.row]
    
            let cell: CustomCellA = collectionView.dequeueReusableCellWithReuseIdentifier("demoCell", forIndexPath: indexPath) as! CustomCellA
        
            if indexPath.section == 0 {
                cell.cellTitle.text = Name
                cell.imgCell.image = UIImage(named: Pics[indexPath.row])
            
                cell.imgCell.layer.masksToBounds = true
                cell.self.imgCell.layer.cornerRadius = 20
                
                return cell
                
                
            } else {
            
                let cell2: AddCell = collectionView.dequeueReusableCellWithReuseIdentifier("demoCell2", forIndexPath: indexPath) as! AddCell
                return cell2
            }
        } else if collectionView == self.EmojiCollectionViewController {
            let cellB: CustomCellB = collectionView.dequeueReusableCellWithReuseIdentifier("demoCellB", forIndexPath: indexPath) as! CustomCellB
            
            cellB.MyLabel.text = arrayOne[indexPath.row]
            
            return cellB
            
        } else {
            let cellC: CustomCellC = collectionView.dequeueReusableCellWithReuseIdentifier("demoCellC", forIndexPath: indexPath) as! CustomCellC
            
            // ...Set up cell
            let height = self.CollectionViewController2.frame.height
            
            cellC.frame = CGRectMake(cellB.frame.origin.x, 0, cellB.frame.size.width, height)
            cellC.updateConstraintsIfNeeded()
            cellC.layoutIfNeeded()
            cellC.imgVw.image = UIImage(named: pictures[indexPath.row] as! String)

            return cellC
        }
        
    }

    
    func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
        
        if collectionView == self.CollectionViewController {
        
            if indexPath.section == 0 {
                let cell: CustomCellA = CustomCellB()
                cell.MyLabel.backgroundColor = UIColor.blueColor()  //crashes due to nil value)

            }
        
        } else {
            
            
        }
        
        
    }

我尝试在 didHighlight 中使用类似的定义,但它一直在崩溃。

【问题讨论】:

    标签: ios swift cell collectionview


    【解决方案1】:

    didHighlightItemAtIndexPath 只改变数据,不改变视图。因此,将friends[indexPath.row] 设为对象或向元组添加另一个参数。在didHighlightItemAtIndexPath 中执行以下操作:

     if collectionView == self.CollectionViewController {
         if indexPath.section == 0 {
            let (fname, lname, color) = friends[indexPath.row];
    
            friends[indexPath.row] = (fname, lname, UIColor.blueColor())
         }     
     }
    

    cellForItemAtIndexPath:

    if collectionView == self.CollectionViewController {
        let (FriendFirstName, FriendLastName, color) = friends[indexPath.row]
    
        if indexPath.section != 0 {
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("demoCell2", forIndexPath: indexPath) as! AddCell;
            return cell;
        } else if color == nil {
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("demoCell", forIndexPath: indexPath) as! CustomCellA;
    
            cell.cellTitle.text = Name
            cell.imgCell.image = UIImage(named: Pics[indexPath.row])
    
            cell.imgCell.layer.masksToBounds = true
            cell.self.imgCell.layer.cornerRadius = 20
    
            return cell
        } else {
            cell = collectionView.dequeueReusableCellWithReuseIdentifier("demoCellB", forIndexPath: indexPath) as! CustomCellB;
    
            // your code for CustomCellB
    
            return cell;
        }
    
    } 
    

    编辑:已更新,因此它使用元组代替对象。还添加了您需要的功能。基本上,您需要在界面构建器中创建两个具有不同重用标识符和类的原型单元。然后将索引路径中的正确标识符出列。另外,我重构了你的一些代码,如果我是你,我会为每个 collectionView 创建一个不同的函数并执行以下操作:

    if collectionView == self.CollectionViewController {
        return self.dequeueCollectionCell(indexPath);
    } else if collectionView == self.EmojiCollectionViewController {
        return self.dequeuEmojiCell(indexPath);
    } else {
        return self.dequeueSomeOtherCell(indexPath);
    }
    

    另外,您提供的代码...我希望它不是实际的生产代码,并且您更改了此论坛的值。否则,即使在几天之内,你也会迷失在这里发生的事情。太多不一致的变量名称和标识符。

    还有一个。在类名中使用命名约定。阅读此forum post 了解更多信息。苹果在任何地方都使用骆驼案。在大多数情况下,类名的首字母大写,而不是对象名。

    【讨论】:

    • 问题是我在 collectionView 单元格中有一个 UIView。由于插座在一个单独的类中,我不能使用 cell.myView.backgroundColor 因为我无法定义它。
    • 另外你不能为字符串或数组设置颜色,那只是数据源,而不是 UILabel。
    • 我知道你不能这就是为什么我说交朋友[indexPath.row] 一个对象或元组(我使用了对象)
    • 我不明白你为什么要这么做。那只是自找麻烦。您已在界面构建器中将单元格的类设置为 CustomCellA ,对吗?因此,当您这样做时,您将遇到选角问题。但是有一种方法可以实现这一点。在界面生成器中创建两个原型单元格,然后通过cellForRowAtIndexPath
    【解决方案2】:

    首先你必须定义collectionView Cell 然后在那个cell 上做任何你想做的事情。要定义您的销售,请将以下几行添加到 didHighlightItemAtIndexPath

    if let cellToUpdate = self.dataCollection.cellForItemAtIndexPath(indexPath) {
                                    //your code here.
                                }
    

    【讨论】:

    • 这不会打开自定义类的对象。当单元格突出显示时,我需要更改单元格内对象的框架。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多