【发布时间】:2017-03-02 10:37:02
【问题描述】:
这里有部分名称:
- 我们喜爱的新游戏
- 我们喜欢的新应用
两者都有水平滚动并且相互独立。
我的问题是我应该使用以下两个选项来实现这一点的可能方式是什么。请提供任何参考样本(如果有):
- 我可以使用单个
UICollectionView并具有不同的部分(动态)来实现相同的行为。但是不同部分的滚动应该是独立的。因为第 1 部分可能有不同数量的项目(行),而第 2 部分可能有不同数量的项目(行) - 我是否必须以编程方式获取多个collectionview 然后插入uiscrollview(垂直滚动视图)。 abd 然后定义水平滚动并通过标记 collectionview 在单元格中添加项目。
我目前已经通过以下代码完成了带有水平滚动的collectionview:
- (void)viewDidLoad {
[super viewDidLoad];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
_collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[_collectionView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:_collectionView];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark Collection View Methods
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 15;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor=[UIColor greenColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(50, 50);
}
请帮忙。
【问题讨论】:
-
嘿@ios 开发者。这可能会对您有所帮助:ashfurrow.com/blog/…
-
我建议你使用
storyboards来实现这个。 -
@Adeel 感谢您的评论。我想使用故事板。但是我该如何实现呢?通过采取多个collectionview ?或者一个具有不同部分的集合视图。我需要独立滚动。所以我想我必须采取多个滚动视图。
-
@iosdeveloper 假设您使用 swift 作为编程语言,我已经发布了答案。希望它能解决您的所有顾虑和需求。
-
@check 完整教程代码9to5ios.com/…
标签: ios iphone uicollectionview uicollectionviewcell