CollectionView对象(Swift)

时间:2015-04-24 22:05:52

标签: ios swift cell collectionview

我希望突出显示可以更改集合视图中对象的大小和外观。

如何在" 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中使用类似的定义并且它一直在崩溃。

2 个答案:

答案 0 :(得分: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。 Apple到处都使用camelCase。在大多数情况下,第一个字母大写为类名,而不是对象名。

答案 1 :(得分:0)

首先你必须定义collectionView Cell然后在那个单元格上做你想做的事情。定义您的销售将以下行添加到didHighlightItemAtIndexPath

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