gpt4 book ai didi

ios - 我如何像这张图片一样创建 Collection View

转载 作者:行者123 更新时间:2023-11-28 19:41:02 25 4
gpt4 key购买 nike

enter image description here

我需要前两个小单元格,第二个大单元格,再两个小单元格,两个大单元格。我曾尝试使用以下代码。但它没有给我正确的输出。否则任何易于为此实现的第三方库。急切地等待这个问题的答案。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if (collectionView == _CollectionSale) {
if (small && j != 2){
j++;
if(j==2){
small = false;
k = 0;

}
HomeSaleCC *objHomeSaleCC=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeSaleCC" forIndexPath:indexPath];
return objHomeSaleCC;
}else if(!small && k !=2){
k++;
if(k==2){
small = true;
j = 0;
}
HomeSaleCC2 *objHomeSaleCC2=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeSaleCC2" forIndexPath:indexPath];
return objHomeSaleCC2;
}

HomeSaleCC *objHomeSaleCC=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeSaleCC" forIndexPath:indexPath];
return objHomeSaleCC;


}
else{

HomeTabCC *objHomeTabCC=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeTabCC" forIndexPath:indexPath];
[objHomeTabCC.btnSale setTitle:[arrTitle objectAtIndex:indexPath.row] forState:UIControlStateNormal];
[objHomeTabCC.btnSale setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
if (indexPath.row == i) {
objHomeTabCC.viewLine.hidden =NO;
objHomeTabCC.btnSale.selected =YES;
}
else{
objHomeTabCC.viewLine.hidden =YES;
objHomeTabCC.btnSale.selected =NO;
}
return objHomeTabCC;
}
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if (collectionView == _CollectionSale) {
if (small && j != 2){
j++;
if(j==2){
small = false;
k = 0;
}
return CGSizeMake(153, 186);
}

else if(!small && k !=2){
k++;
if(k==2){
small = true;
j = 0;
}
return CGSizeMake(260, 186);
}else{
return CGSizeMake(260, 186);
}

}
else{
return CGSizeMake(260, 186);
}
}

最佳答案

支持@beyowulf 的回答我为这个问题写了一些代码,你可以find that here ,检查 CollectionViewTest 部分:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

return 7;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"smallCell" forIndexPath:indexPath];
[cell.contentView setBackgroundColor:[UIColor redColor]];

if (indexPath.row%4 < 2) {
[cell.contentView setBackgroundColor:[UIColor greenColor]];
}
return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 4 < 2) {
return CGSizeMake([UIScreen mainScreen].bounds.size.width / 2 - 15.0f, 80.0f);
}
return CGSizeMake([UIScreen mainScreen].bounds.size.width / 2 - 15.0f, 200.0f);
}

它看起来像这样:

enter image description here

关于ios - 我如何像这张图片一样创建 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34241318/

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