2015-04-24 34 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