Ios 在UICollectionView中每个部分显示两个单元格
我使用UICollectionView来显示图像,我存储在这样的数组中Ios 在UICollectionView中每个部分显示两个单元格,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我使用UICollectionView来显示图像,我存储在这样的数组中 newsPhotos = [NSArray arrayWithObjects:@"photo1.png", @"photo2.png", @"photo3.png", @"photo4.png", @"photo5.png", @"photo6.png", @"photo7.png", @"photo8.png", @"photo9.png", @"photo10.png", Nil]; 我的想法是按此顺序显示网格 1 |
newsPhotos = [NSArray arrayWithObjects:@"photo1.png", @"photo2.png", @"photo3.png", @"photo4.png", @"photo5.png", @"photo6.png", @"photo7.png", @"photo8.png", @"photo9.png", @"photo10.png", Nil];
我的想法是按此顺序显示网格
1 | 2
3 | 4
5 | 6
7 | 8
9 | 10
因此,我的UICollectionView方法按行显示两张照片,如下所示:
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return 2;
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return newsPhotos.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[newsPhotos objectAtIndex:indexPath.section]];
cell.textLabel.text = @"Data";
return cell;
}
但它是这样印刷的
1 | 1
2 | 2
3 | 3
etc..
我理解它为什么会发生,只是不知道如何修复它。有什么帮助吗
谢谢尝试添加它,以便:
recipeImageView.image = [UIImage imageNamed:[newsPhotos objectAtIndex:indexPath.row + (indexPath.section *2)]];
你这样做会得到什么
因为你想做的是:
section == 0 + row == 0 *2 [0] | section == 1 + row == 0 *2 [1]
section == 0 + row == 1 *2 [2] | section == 1 + row == 1 *2 [3]
section == 0 + row == 2 *2 [4] | section == 1 + row == 2 *2 [5]
section == 0 + row == 3 *2 [6] | section == 1 + row == 3 *2 [7]
很抱歉,我在做这件事,我只是测试了一下,试试看,它对你有用。确保将滚动方向设置为垂直方向。尝试添加滚动方向,以便:
recipeImageView.image = [UIImage imageNamed:[newsPhotos objectAtIndex:indexPath.row + (indexPath.section *2)]];
你这样做会得到什么
因为你想做的是:
section == 0 + row == 0 *2 [0] | section == 1 + row == 0 *2 [1]
section == 0 + row == 1 *2 [2] | section == 1 + row == 1 *2 [3]
section == 0 + row == 2 *2 [4] | section == 1 + row == 2 *2 [5]
section == 0 + row == 3 *2 [6] | section == 1 + row == 3 *2 [7]
很抱歉,我在做这件事,我只是测试了一下,试试看,它对你有用。但请确保将滚动方向设置为垂直。问题在于,您查看的是
indexath.section
,而不是indexath.row
,因为每个部分都有两个项目,所以它会像您在那里看到的那样重复
您可以尝试使用一个部分,然后改用[newsphots objectAtIndex:indexath.row]
。还可以使用UICollectionViewFlowLayoutDelegate更改布局,如下所示:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(CGRectGetWidth(collectionView.frame) * 0.5f, cellHeightHere);
}
您可以看到一个更详细的答案。问题是您看到的是
indexPath.section
而不是indexPath.row
,因为每个部分有两个项目,所以它会像您看到的那样重复
您可以尝试使用一个部分,然后改用[newsphots objectAtIndex:indexath.row]
。还可以使用UICollectionViewFlowLayoutDelegate更改布局,如下所示:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(CGRectGetWidth(collectionView.frame) * 0.5f, cellHeightHere);
}
您可以看到更详细的答案。试试这个
-(NSInteger)numberOfSectionsInCollectionView:
(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return newsPhotos.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// Try and use custom cell by placing a custom cell on your collection view,assign a new class to that cell and then configure that cell as
customCellClass *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
// also place an image view on your custom cell, make an outlet of that image view in your custom cell class and use:
myCell.imageview.image = [UIImage imageNamed:[newsPhotos objectAtIndex:indexPath.row]];
return myCell;
}
现在,要每行显示两个图像,您可以相应地调整单元格和图像视图的大小,使其每行仅显示两个图像。
希望这对你有所帮助试试这个
-(NSInteger)numberOfSectionsInCollectionView:
(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return newsPhotos.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// Try and use custom cell by placing a custom cell on your collection view,assign a new class to that cell and then configure that cell as
customCellClass *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
// also place an image view on your custom cell, make an outlet of that image view in your custom cell class and use:
myCell.imageview.image = [UIImage imageNamed:[newsPhotos objectAtIndex:indexPath.row]];
return myCell;
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let size = collectionView.bounds.size.width / 2
return CGSize(width: size, height: 262)
}
现在,要每行显示两个图像,您可以相应地调整单元格和图像视图的大小,使其每行仅显示两个图像。
希望这将有助于您在开始时重复上一张图像1 | 2 2 | 3 | 4 | 4 | 5 `现在它显示为1 | 3 2 | 4并引发此异常
***由于未捕获的异常“NSRangeException”终止应用程序,原因:'***-[\uu NSArrayI objectAtIndex::]:索引8超出边界[0..7]'***第一次引发调用堆栈:
不应该,尝试将*2放在indexath.section旁边?我在做这件事,我从来没有使用过uicollectionviews,但它应该可以工作。recipeImageView.image=[UIImage ImageName:[newsPhotos objectAtIndex:(indexPath.section*2)+indexPath.row]];如果indexath.row增加并且没有交替,您也可以尝试执行:[objectAtIndex:indexath.section+indexPath.row%2],我只是想私下告诉您,如果我在兜圈子,我很抱歉。您能把NSLog(@“row:%d sec:%d”,indexPath.row,indexPath.section)放在下面吗;在cell.textlab.text=@“数据”之后;告诉我你得到了什么。最后一张图片在开头重复了一遍1 | 2 2 | 3 | 4 | 4 | 5 `现在它显示为1 | 3 2 | 4并引发此异常***由于未捕获的异常“NSRangeException”终止应用程序,原因:'***-[\uu NSArrayI objectAtIndex::]:索引8超出边界[0..7]'***第一次引发调用堆栈:
不应该,尝试将*2放在indexath.section旁边?我在做这件事,我从来没有使用过uicollectionviews,但它应该可以工作。recipeImageView.image=[UIImage ImageName:[newsPhotos objectAtIndex:(indexPath.section*2)+indexPath.row]];如果indexath.row增加并且没有交替,您也可以尝试执行:[objectAtIndex:indexath.section+indexPath.row%2],我只是想私下告诉您,如果我在兜圈子,我很抱歉。您能把NSLog(@“row:%d sec:%d”,indexPath.row,indexPath.section)放在下面吗;在cell.textlab.text=@“数据”之后;告诉我你得到了什么。如何使它对所有设备通用。如何使它对所有设备通用。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let size = collectionView.bounds.size.width / 2
return CGSize(width: size, height: 262)
}