gpt4 book ai didi

ios - 如何使用MDCardCollectionCell? [iOS Material 组件]

转载 作者:行者123 更新时间:2023-11-30 11:16:40 24 4
gpt4 key购买 nike

我是 Material 设计新手。我在 Main.storyboard 中有自定义的 collectionView 单元格,其中包含一些标签、按钮和 Imageview。我想将自定义单元格加载为 MDCCardCollectionCell

当我使用此代码时,我得到空的MDCCardCollectionCell。它使应用程序崩溃

collectionView.register(MDCCardCollectionCell.self, forCellWithReuseIdentifier: "Cell")

func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell",
for: indexPath) as! MDCCardCollectionCell

cell.cornerRadius = 8
cell.setShadowElevation(6, for: .selected)
cell.setShadowColor(UIColor.black, for: .highlighted)
return cell
}

当我在没有此行的情况下加载自定义 collectionView 单元格时,它已从 Main.storyboard 成功加载,但 MDCCard 样式未应用(阴影效果)。

collectionView.register(MDCCardCollectionCell.self, forCellWithReuseIdentifier: "Cell")

谢谢

最佳答案

以下应该有效:

swift :

collectionView.register(MDCCardCollectionCell.self, forCellWithReuseIdentifier: "Cell")

func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell",
for: indexPath) as! MDCCardCollectionCell
// If you wanted to have the card show the selected state when tapped
// then you need to turn isSelectable to true, otherwise the default is false.
cell.isSelectable = true

cell.selectedImageTintColor = .blue
cell.cornerRadius = 8
cell.setShadowElevation(6, for: .selected)
cell.setShadowColor(UIColor.black, for: .highlighted)
return cell
}
<小时/>

目标:

[self.collectionView registerClass:[MDCCardCollectionCell class]
forCellWithReuseIdentifier:@"Cell"];

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MDCCardCollectionCell *cell =
[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell"
forIndexPath:indexPath];
// If you wanted to have the card show the selected state when tapped
// then you need to turn selectable to true, otherwise the default is false.
[cell setSelectable:YES];

[cell setSelectedImageTintColor:[UIColor blueColor]];
[cell setCornerRadius:8];
[cell setShadowElevation:6 forState:MDCCardCellStateSelected];
[cell setShadowColor:[UIColor blackColor] forState:MDCCardCellStateHighlighted];
}

关于ios - 如何使用MDCardCollectionCell? [iOS Material 组件],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51682880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com