简体   繁体   English

如何使用UICollectionView制作这种视图?

[英]How to make this kind of view using UICollectionView?

I need help. 我需要帮助。 How can I design this kind of view 我该如何设计这种观点

在此输入图像描述

I'm using UICollectionView for this but all cell comes in a sequence ie 4 cell in each row. 我正在使用UICollectionView ,但所有单元格都按顺序排列,即每行4个单元格。 This Data is coming from json . 这个数据来自json So that array size is not fixed. 因此数组大小不固定。 Please help me how can I do this. 请帮帮我怎样才能做到这一点。 Thanks in advance... 提前致谢...

You can create like this by using the custom collection view something like same as reference link in comment . 您可以使用自定义集合视图创建这样的内容,就像注释中的引用链接一样。

for handling the this design- 处理这个设计 -

create a grid by using two diminutional arrey and calculate the row odd and even and create like same view. 通过使用两个缩小arrey来创建网格并计算奇数和偶数行并创建相同的视图。

After trying a couple of hours, here is my own logic to make this kind of view which is small and easy to understand. 在尝试了几个小时之后,这是我自己的逻辑,使这种视图小而易于理解。 Hope this will solve your problem and let me know if you have any confusion. 希望这能解决您的问题,如果您有任何困惑,请告诉我。

Try this- 尝试这个-

Take two global NSMutableArray and an int variable ie 取两个global NSMutableArray和一个int变量ie

NSMutableArray *jsonArray; // holds sequence of value i.e. 1,2,3.....,23
NSMutableArray *sectorArray; // holds sector array
int numOfSection; // holds numberOfSections

here is the main logic in viewDidLoad() - 这是viewDidLoad()主要逻辑 -

- (void)viewDidLoad {

    [super viewDidLoad];

    sectorArray=[[NSMutableArray alloc]init];

    jsonArray=[[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23", nil];

    numOfSection= (int)ceilf(jsonArray.count/4.0); //devide jsonArray into section by deviding minimum cell in a row i.e. 4 for ex. 23/4 =5.75 and after ceilf() it is 6.

    int rangeVar=0; // check range for splitting jsonArray into sector.

    for (int i=0; i<numOfSection; i++) { // for loop will run 6 times i.e. 0-5

        if (rangeVar<jsonArray.count) { // check range must be less then jsonArray.count otherwise it crash the app

            if (i%2==0) { //for even/odd secionArray

                if (jsonArray.count>rangeVar+4) { // check jsonArroy count is gretter than rangeVar+4 otherwise add remaining values. Below i have evaluated jsonArray how this work.

                    [sectorArray addObject:[jsonArray subarrayWithRange:NSMakeRange(rangeVar, 4)]];//j=0-- 1234 j=2-- 10111213 j=4 19202122

                }else{

                    [sectorArray addObject:[jsonArray subarrayWithRange:NSMakeRange(rangeVar, jsonArray.count-rangeVar)]];

                }

                rangeVar+=4; // for next time range start from 4

            }else{

                if (jsonArray.count>rangeVar+5) {

                    [sectorArray addObject:[jsonArray subarrayWithRange:NSMakeRange(rangeVar, 5)]];// j=1-- 56789 j=3 1415161718 j=5 23

                }else{

                    [sectorArray addObject:[jsonArray subarrayWithRange:NSMakeRange(rangeVar, jsonArray.count-rangeVar)]];

                }

                rangeVar+=5;
            }

        }else{

            numOfSection-=1; // if you have 27 items than numOfSection is 7 but item 6 section are enough to hold all items.
        }

    }

    [self.collectionView reloadData];

}

//collectionView dataSource/Delegate methods

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    UILabel *lblNumber=[[UILabel alloc]initWithFrame:cell.contentView.frame];
    lblNumber.layer.cornerRadius=lblNumber.frame.size.height/2;
    lblNumber.layer.borderWidth=2.0;
    lblNumber.layer.borderColor=[UIColor grayColor].CGColor;
    lblNumber.layer.masksToBounds=YES;
    lblNumber.text=[NSString stringWithFormat:@"%@",[[sectorArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
    lblNumber.textAlignment=NSTextAlignmentCenter;
    [cell.contentView addSubview:lblNumber];

    return cell;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return numOfSection;
}


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

    return [[sectorArray objectAtIndex:section] count];
}

// horizontally center align all cells

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    NSInteger cellCount = [collectionView.dataSource collectionView:collectionView numberOfItemsInSection:section];
    if( cellCount >0 )
    {
        CGFloat cellWidth = ((UICollectionViewFlowLayout*)collectionViewLayout).itemSize.width+((UICollectionViewFlowLayout*)collectionViewLayout).minimumInteritemSpacing;
        CGFloat totalCellWidth = cellWidth*cellCount;
        CGFloat contentWidth = collectionView.frame.size.width-collectionView.contentInset.left-collectionView.contentInset.right;
        if( totalCellWidth<contentWidth )
        {
            CGFloat padding = (contentWidth - totalCellWidth) / 2.0;
            return UIEdgeInsetsMake(10, padding, 0, padding);
        }
    }
    return UIEdgeInsetsZero;
}

the main logic behind this is to split array into subarray and make different section as number of subarrays and add all subarrays into one mutableArray . 这背后的主要逻辑将阵列分成子阵列,使不同section作为数subarrays ,并添加所有subarrays成一个mutableArray Than work with this mainArray. 比使用这个mainArray。

Here is the output: 这是输出:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何制作“卡片叠”类的UICollectionView? - How do I make a 'card stack' kind of UICollectionView? 如何使用Alamofire使图像在UICollectionView单元格中以其标签显示在视图中? - How to make images appear in the view with its labels in UICollectionView cells using Alamofire? 如何在iOS中使用UICollectionView创建附加视图 - How to create attached view using UICollectionView in iOS 如何使用 NSTimer 使 UICollectionView 自动滚动? - How to make UICollectionView auto scroll using NSTimer? 如何使UICollectionView的某个部分中的所有项目浮动在滚动视图上? - How to make all items in a section of a UICollectionView float over the scroll view? iOS UICollectionView-如何使第一个单元格出现在集合视图的底部? - iOS UICollectionView - how to make first cell appear at the bottom of the collection view? 如何使用UICollectionView制作网格 - How to make a grid with UICollectionView 如何制作这种动画? - How to make this kind of animation? 如何在不使用情节提要的情况下在iOS Swift 3中以编程方式创建UICollectionView类? - How to make UICollectionView class programmatically in iOS Swift 3 without using storyboard? UICollectionView视图和约束以使单元格适合 - UICollectionView view and constraints to make cells fit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM