gpt4 book ai didi

ios - 在 collectionView cellForItemAtIndexPath 中创建形状 - Objective C

转载 作者:行者123 更新时间:2023-11-28 20:04:00 25 4
gpt4 key购买 nike

我正在从事我的一个项目,该项目根据从 MySQL 数据库返回的数据填充 5 个项目。我的问题是我需要根据返回的数据将每个项目设为圆形或方形。

运行以下命令时:

UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
_collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];

[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[_collectionView setBackgroundColor:[UIColor whiteColor]];
CGRect frame = [_collectionView frame];
[_collectionView setFrame:CGRectMake(10,
5,
300,
frame.size.height - 100)];
[self.view addSubview:_collectionView];

..调用创建正方形的自定义布局。我的问题是如何在单元格创建函数中将其变成正方形或圆形?

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
.... do my creation of circle or square in here
}

而不是在这里:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(50, 50);
}

最佳答案

您可以子类化 UICollectionViewCell 并将绘图代码放在子类中,在 drawRect:

一旦你创建了实际的类,你就可以通过做类似的事情来使用它

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier";
// set custom properties here
return cell;
}

关于ios - 在 collectionView cellForItemAtIndexPath 中创建形状 - Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22751661/

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