Ios 如何创建如下图所示的集合视图
我需要前两个小房间,后两个大房间,再两个小房间,两个大房间。 我试过使用下面的代码。但它并没有给我正确的输出。否则任何第三方库都很容易实现。急切地等待这个问题的答案Ios 如何创建如下图所示的集合视图,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我需要前两个小房间,后两个大房间,再两个小房间,两个大房间。 我试过使用下面的代码。但它并没有给我正确的输出。否则任何第三方库都很容易实现。急切地等待这个问题的答案 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ if (collectionView == _CollectionSale)
- (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);
}
}
我不确定您的代码的其余部分在做什么,但如果您只需要两个小单元格,然后两个大单元格,您可以这样做:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.item % 4 < 2) {
return CGSizeMake(collectionView.bounds.size.width*0.5-someSpacing, 186);
}
return CGSizeMake(collectionView.bounds.size.width*0.5-someSpacing, 186*2);
}
-(CGSize)collectionView:(UICollectionView*)collectionView布局:(UICollectionView布局*)collectionView布局大小格式化索引XPath:(nsindepath*)索引XPath{
if(indexath.item%4<2){
返回CGSizeMake(collectionView.bounds.size.width*0.5-someSpacing,186);
}
返回CGSizeMake(collectionView.bounds.size.width*0.5-someSpacing,186*2);
}
虽然我建议返回我不确定您的代码的其余部分在做什么,但是如果您只需要两个小单元格,然后是两个大单元格,那么您可以这样做:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.item % 4 < 2) {
return CGSizeMake(collectionView.bounds.size.width*0.5-someSpacing, 186);
}
return CGSizeMake(collectionView.bounds.size.width*0.5-someSpacing, 186*2);
}
-(CGSize)collectionView:(UICollectionView*)collectionView布局:(UICollectionView布局*)collectionView布局大小格式化索引XPath:(nsindepath*)索引XPath{
if(indexath.item%4<2){
返回CGSizeMake(collectionView.bounds.size.width*0.5-someSpacing,186);
}
返回CGSizeMake(collectionView.bounds.size.width*0.5-someSpacing,186*2);
}
尽管我建议返回支持@beyowulf回答的我为这个问题编写了一些代码,但您可以查看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);
}
-(NSInteger)SectionsInCollectionView:(UICollectionView*)collectionView{
返回1;
}
-(NSInteger)collectionView:(UICollectionView*)collectionView项目编号截面:(NSInteger)截面{
返回7;
}
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath{
UICollectionViewCell*单元格=[collectionView dequeueReusableCellWithReuseIdentifier:@“smallCell”forIndexPath:indexPath];
[cell.contentView setBackgroundColor:[UIColor redColor]];
if(indexPath.row%4<2){
[cell.contentView setBackgroundColor:[UIColor greenColor]];
}
返回单元;
}
-(CGSize)collectionView:(UICollectionView*)collectionView布局:(UICollectionViewLayout*)collectionViewLayout大小FormiteIndeXPath:(NSIndexPath*)indexPath{
if(indexath.row%4<2){
返回CGSizeMake([UIScreen mainScreen].bounds.size.width/2-15.0f,80.0f);
}
返回CGSizeMake([UIScreen mainScreen].bounds.size.width/2-15.0f,200.0f);
}
它看起来像这样:
支持@beyowulf的答案我为这个问题编写了一些代码,您可以查看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);
}
-(NSInteger)SectionsInCollectionView:(UICollectionView*)collectionView{
返回1;
}
-(NSInteger)collectionView:(UICollectionView*)collectionView项目编号截面:(NSInteger)截面{
返回7;
}
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath{
UICollectionViewCell*单元格=[collectionView dequeueReusableCellWithReuseIdentifier:@“smallCell”forIndexPath:indexPath];
[cell.contentView setBackgroundColor:[UIColor redColor]];
if(indexPath.row%4<2){
[cell.contentView setBackgroundColor:[UIColor greenColor]];
}
返回单元;
}
-(CGSize)collectionView:(UICollectionView*)collectionView布局:(UICollectionViewLayout*)collectionViewLayout大小FormiteIndeXPath:(NSIndexPath*)indexPath{
if(indexath.row%4<2){
返回CGSizeMake([UIScreen mainScreen].bounds.size.width/2-15.0f,80.0f);
}
返回CGSizeMake([UIScreen mainScreen].bounds.size.width/2-15.0f,200.0f);
}
它看起来像这样:
您的启动错误-不应使用固定大小,而应根据要显示的图像大小使用动态大小。更好的变量命名,小的,k和j在做什么?我不需要图像大小的单元格,我需要固定的前两个小的,而不是两个大的。实际上,您只需阅读如何使用集合视图委托方法。剩下的简单明了。你能帮我添加一些代码吗?你的起点错了-你不应该使用固定尺寸,而应该使用动态尺寸,这取决于要显示的图像的大小。更好的变量命名,小的,k和j在做什么?我不需要图像大小的单元格,我需要固定的前两个小的,而不是两个大的。实际上,您只需阅读如何使用集合视图委托方法。剩下的简单明了。你能帮我添加一些代码吗?