Re: [問題] collection view cell 初始化

看板MacDev作者 (白毛)時間11年前 (2013/09/26 17:26), 編輯推噓0(002)
留言2則, 1人參與, 最新討論串3/3 (看更多)
如果你的view是從xib或storyboard讀進來的話 它會跑initWithCoder initWithCoder: - Implement this method if you load your view from an Interface Builder nib file and your view requires custom initialization. UIView Class Reference 搜尋Methods to Override就會看到了 https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html EX: - (id)initWithCoder:(NSCoder *)aDecoder { if(self = [super initWithCoder:aDecoder]) { self.xxx = oooo; } return self; } ※ 引述《issuemylove (skill)》之銘言: : 想請問各位大大 : 因為 cell 會重複使用 : 但是有些東西每個 cell 都是一樣的 (只須設定一次) : 例如: cell 的背景顏色、邊框圓角等 : 我想要只設定一次 而不是傻傻地在 cellForItemAtIndexPath 裡每次都設定 : 即是在自訂的cell中 實作 -(id)initWithFrame : 請問有甚麼好方法嗎? : 我目前是 viewDidLoad 使用 : [collectionView registerNib: forCellWithReuseIdentifier: ] : 然後在 cellForItemAtIndexPath 裡面做設定 : 但這樣並不會觸發自訂cell的 initWithFrame : 有試過這篇的方法http://stackoverflow.com/questions/17406186/ 使用 registerClass : 的確是會觸發 initWithFrame 但是就我的cell 卻離奇的消失不顯示 只是透明 : 請問我該怎麼辦才好QQ : 還是說有甚麼其他方法呢? : 謝謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.36.63.52 ※ 編輯: whitefur 來自: 114.36.63.52 (09/26 17:38)

09/27 10:52, , 1F
謝謝! 成功了XD 簡單好用 也不用塞到 cellForItemXX裡
09/27 10:52, 1F

09/27 10:52, , 2F
小弟還是個新手 問新手問題還請多多海涵QQ
09/27 10:52, 2F
文章代碼(AID): #1IG_t2Zg (MacDev)
文章代碼(AID): #1IG_t2Zg (MacDev)