I want to develop the screen(objective C) like this :
In this there are section names :
- New Games we Love
- New Apps We Love
Both have horizontal scroll and independent to each other.
My question is what is the possible way i should use to implement this from below two option. Please provide me any reference sample if available:
- Can i achieve the same behaviour withe single
UICollectionView
and having different section (dynamic). But the scroll for different section should be independent. Because it may possible that section 1 may have different number of items(rows) and section 2 may have different number of items (rows) - Do i have to take multiple collectionview programmatically and insert then in the uiscrollview (vertical scrollview). abd then define the horizontal scroll and add the items in the cell by taging the collectionview.
I had done the collectionview with the horizontal scrolling by below code at present :
- (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);
}
Please help.
Workaround (Using UICollectionView with Swift 4)
I am facing the same issue and can't find a way to make it work with
UICollectionViewFlowLayout
.If you want to use
UICollectionView
, but the SupplementaryView is not flexible enough, you can do this workaround:I have a fixed
UILabel
in the top-left side of theCollectionView
and one floatingUILabel
that will move to the right accordingly. If there is only one section visible the second label will be hiddenWe can intercept all cells being created/dequeued in the
collectionView:cellForItemAt indexPath:
method, but will will give you a lot of cell frames (many of them being prepared but still offscreen, and you will have to manage what frame corresponds to what section. This can be tricky if you have multiple rows in your horizontal scrolling collection viewYou need to intercept the sections and the cell frames to found the lowest x coordinate, and the most reliable way is to inspect the
.visibleCells
property of the collectionView. This may be a lot of iterations (depending on the number of visible cells) and I'd love to identify a more efficient way. (a delegate method, a CollectionViewCellsDidAppear kind of notification, or being able to add an Observer to the .visibleCells property (not possible given that it is read-only)So far this works (Swift 4), but has room for improvement:
This code will give you a Dictionary with visible sections (keys) and the lowest x coordinate for each section (value) to align the floating label2 (my label1 has a fixed x at the left of the collectionView). You can also animate, hide/show the labels accordingly
This workaround is not optimal and I will post additional information if a solution using
UICollectionViewFlowLayout
What you are trying to do is not that difficult. I have created a prototype of what you are look at. This is how would your
storyboard's view controller
and its document outline look like:Here's the code for each component
TableViewController
MyTableViewCell
MyCollectionViewCell
This is how it looks like on the simulator