[英]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.