2015-01-05 85 views
0

想象的場景,你有一個UICollectionView,並在每個單元格你想有一個按鈕,填滿整個細胞,這樣就可以對各種觸摸事件應對處理的高亮外觀。例如,當用戶觸摸按鈕時,您想要更改按鈕的背景顏色,然後在拖出或取消觸摸等時將其還原。現在想象一下,您不需要更改要更改的按鈕背景顏色該單元的backgroundView。 A UIButton沒有背景視圖,只有backgroundColorbackgroundImage使用selectedBackgroundView顯示高亮狀態

我有一個解決方案,但我不知道它是否可以更清潔,如果這種方法不建議。一旦觸摸按鈕I循環其superview s直到我得到UICollectionViewCell然後我將其selected屬性設置爲true。在cellForItemAtIndexPath我根據需要設置了selectedBackgroundView。這樣可以獲得所需的行爲,但使用選定狀態來指示突出顯示狀態並以此方式管理它是不恰當的?什麼會更好?

我可以在觸摸一個按鈕,然後改變其backgroundView屬性,而不是創建每個單元格時做這件事得到UICollectionViewCell,那麼就沒有必要改變selected值。但是,這仍然不是一個很好的解決方案。

+0

與使用UILabel代替UIButton的普通UICollectionViewCell有什麼不同,那麼只需更改單元格的contentView的backgroundColor?您的按鈕似乎沒有做任何特別的事情,並且注意到集合或表視圖單元格內的按鈕會延遲其向下觸摸動畫/高光,因爲單元格本身會首先檢查選擇事件。 –

+0

@Matt事實上,按鈕並不特別,它存在的唯一原因是因爲'UICollectionViewCell'不是'UIControl',因此您無法檢測到TouchDown,TouchDragEnter,TouchCancel等,因此無法在出現時更改外觀感動。你可以當它被輕敲('didSelectItemAtIndexPath'),但沒有觸及。 – Joey

回答

2

您不需要集合視圖單元格內的按鈕,只需在按下時設置其高亮顏色即可。只需將單元格的selectedBackgroundView設置爲與您的單元具有相同寬度和高度的視圖,然後爲該視圖指定backgroundColor,以便突出顯示該單元格。

A(髒)實現我所做的是這樣的:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CELL", forIndexPath: indexPath) as UICollectionViewCell 
    cell.selectedBackgroundView = { 
     let bgview = UIView(frame: CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)) 
     bgview.backgroundColor = UIColor.redColor() 
     return bgview 
    }() 
    return cell 
} 

然後,只需取消選擇didSelectItemAtIndexPath細胞。 「壓下」將自動處理,取消動畫僅在用戶擡起手指時觸發。

我認爲這很髒,因爲您每次設置selectedBackgroundView時,該單元在cellForItemAtIndexPath:中被重新使用以供重用。我會做的是創造一個UICollectionViewCell子類,在那裏設置它的selectedBackgroundView,並在集合視圖使用registerNib:registerClass:註冊該單元格。


地址:清潔版。在您的自定義集合視圖細胞亞類,分配backgroundViewselectedBackgroundView

override init(frame: CGRect) { 
    super.init(frame: frame) 

    self.backgroundView = { 
     let view = UIView() 
     view.backgroundColor = UIColor.yellowColor() 
     return view 
    }() 

    self.selectedBackgroundView = { 
     let view = UIView() 
     view.backgroundColor = UIColor.redColor() 
     return view 
     }() 
} 

而且在你看來controlle的相關方法,集合視圖的數據源,並委託:

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.collectionView.registerClass(NSClassFromString("test.CustomCollectionViewCell"), forCellWithReuseIdentifier: "CELL") 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CELL", forIndexPath: indexPath) as UICollectionViewCell 
    return cell 
} 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    collectionView.deselectItemAtIndexPath(indexPath, animated: true) 
} 
+0

糾正我,如果我錯了,但'selectedBackgroundView'只顯示當你以編程方式設置'selected = true',否?所以當你觸摸單元格時,這不會導致該視圖出現。 – Joey

+0

啊,但是當你觸及集合視圖單元格時,選中的內容就變成了真的 - 不需要在代碼中設置它。 :)我會編輯我的答案,以提供更清潔的版本... –

+0

這工作得很好,可能是比我找到的更好的解決方案! – Joey

0

我找到了一個不錯清潔解決方案UICollectionViewCell s已經有一個突出顯示的狀態,可以自動跟蹤發生的各種觸摸事件。但是沒有highlightedBackgroundView就像selectedBackgroundView一樣,但仍然可以利用突出顯示的狀態修改突出顯示和不亮顯示的單元格外觀。因此,不需要在每個單元中使用UIButton - UILabel將會很好。

只需實施幾種UICollectionViewDelegate方法:collectionView:didHighlightItemAtIndexPath:collectionView:didUnhighlightItemAtIndexPath:。突出顯示時,請更改backgroundView屬性,然後將其設置爲nil

+0

每次按住單元格時,不應該改變'backgroundView' - 當單元格*未被選擇時,該屬性應該是背景視圖,即其正常狀態。 –

相關問題