[英]Custom CollectionViewCell With Different Subviews?
我正在創建一個名為MessageCell
的自定義collectionViewCell。 該消息單元包含三個組件, headerLabel
, messageContainerView
和footerLabel
。 問題是,根據消息的類型(視頻,交易,送達確認,照片,文本等),我想顯示具有特定操作等的特定視圖類型。
做到這一點的最佳方法是什么? 我嘗試在我的單元格子類中將容器視圖設置為UIView
,然后根據消息的類型將其設置為與特定的子視圖相等,但這不起作用:
- (void)setMessage:(EMKMessage *)message {
//Set Message
_message = message;
//Check Message Type
switch (message.type) {
case MessageTypeText:
default: {
//Create Message Content View
TextContentView *textContentView = [[TextContentView alloc] initForAutoLayout];
textContentView.frame = CGRectMake(0, 0, 300, 200);
[textContentView setText:message.text];
self.messageContainerView = textContentView;
break;
}
}
}
任何幫助將不勝感激。
您可以單獨創建所需的所有單元。 在
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
您可以根據需要重新分配不同的單元格。 只需檢查要在indexPath
上表示的對象的類型,然后返回相應的單元格即可。 如果需要與這些單元格交互,則這些單元格可以具有委托,或者可以使用塊屬性。 就像是:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
VideoMessageCell *cell = [tableView.dequeueReusableCellWithIdentifier:@"VideoMessageCell"];
//set the cell properties
return cell;
} else if (indexPath.row == 1) {
AudioMessageCell *cell = [tableView.dequeueReusableCellWithIdentifier:@"AudioMessageCell"];
//set the cell properties
return cell;
}
}
現在,我不知道如何確定給定索引所需的單元格類型,但是可以替換indexPath.row
以適合您的需求。 同樣不要忘記相應地設置可重復使用的標識符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.