2015-04-24 33 views
1

我希望突出显示更改集合视图内对象的大小和外观。CollectionView对象(Swift)

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

回答

0

首先,您必须定义collectionView Cell,然后在该单元格上执行任何您想要的操作。定义你的卖出下面的行添加到didHighlightItemAtIndexPath

if let cellToUpdate = self.dataCollection.cellForItemAtIndexPath(indexPath) { 
           //your code here. 
          } 
+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。在大多数情况下,第一个字母大写为类名,而不是对象名。

+0

问题是我有一个UIView里面的collectionView单元格。由于插座位于单独的类中,因此无法使用cell.myView.backgroundColor,因为我无法定义它。 –

+0

此外,您不能将颜色设置为字符串或数组,这只是数据源,而不是UILabel。 –

+0

我知道你不能这就是为什么我说交朋友[indexPath.row]一个对象或元组(我用对象) – Gasim