Hello Ash,
This isn't an issue but rather a request to see if you have any code that would cater towards having different uniform cell sizes of collectionview cells per table index cell in a table section?
If not maybe you can guide me on how to go about doing that? I currently modified your function: setCollectionViewDataSourceDelegate by adding one more parameter which would dictate the cell dimension type:
-(void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate index:(NSInteger)index andCellType: (BOOL) typeCell
Since there are just two types of cells, a billboard and a portrait one, a bool was sufficient to determine the type.
Here is the code i used in the tableviewcontroller. The indexPaths.rows not included below are used to display header data and so were not included in the logic.
- (void)tableView:(UITableView *)tableView willDisplayCell:(ShowsTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 1)
{
[cell setCollectionViewDataSourceDelegate:self index:indexPath.row andCellType:NO];
NSInteger index = cell.collectionViewHorizontal.tag;
CGFloat horizontalOffset = [self.contentOffsetDictionary[[@(index) stringValue]] floatValue];
[cell.collectionViewHorizontal setContentOffset:CGPointMake(horizontalOffset, 0)];
}
else if(indexPath.row == 3)
{
[cell setCollectionViewDataSourceDelegate:self index:indexPath.row andCellType:YES];
NSInteger index = cell.collectionViewHorizontal.tag;
CGFloat horizontalOffset = [self.contentOffsetDictionary[[@(index) stringValue]] floatValue];
[cell.collectionViewHorizontal setContentOffset:CGPointMake(horizontalOffset, 0)];
}
else if(indexPath.row == 5)
{
[cell setCollectionViewDataSourceDelegate:self index:indexPath.row andCellType:YES];
NSInteger index = cell.collectionViewHorizontal.tag;
CGFloat horizontalOffset = [self.contentOffsetDictionary[[@(index) stringValue]] floatValue];
[cell.collectionViewHorizontal setContentOffset:CGPointMake(horizontalOffset, 0)];
}
else if(indexPath.row == 7)
{
[cell setCollectionViewDataSourceDelegate:self index:indexPath.row andCellType:YES];
NSInteger index = cell.collectionViewHorizontal.tag;
CGFloat horizontalOffset = [self.contentOffsetDictionary[[@(index) stringValue]] floatValue];
[cell.collectionViewHorizontal setContentOffset:CGPointMake(horizontalOffset, 0)];
}
else
{
[cell setCollectionViewDataSourceDelegate:self index:indexPath.row andCellType:YES];
NSInteger index = cell.collectionViewHorizontal.tag;
CGFloat horizontalOffset = [self.contentOffsetDictionary[[@(index) stringValue]] floatValue];
[cell.collectionViewHorizontal setContentOffset:CGPointMake(horizontalOffset, 0)];
}
}
In my tableview Cell logic i have this
- (void)awakeFromNib {
[self setFlowLayouts];
self.contentView.backgroundColor = [UIColor blackColor];
}
-(void)setFrame:(CGRect)frame {
[super setFrame:frame];
[self setNeedsDisplay]; // force drawRect:
}
//Happens after setCollectionViewDataSourceDelegate
-(void)layoutSubviews
{
[super layoutSubviews];
if (self.cellType) {
_layout.itemSize = CGSizeMake(170, 249); //portrait images 143.5 x 207.5. Added 12 143.5, 207.5
_layout.sectionInset = UIEdgeInsetsMake(15, 20, 15, 10);//15, 20, 15, 10
} else {
_layout.sectionInset = UIEdgeInsetsMake(15, 20, 15, 10);//left 10
_layout.itemSize = CGSizeMake(300, 169); //billboard images 271, 152
}
self.collectionViewHorizontal.frame = self.contentView.bounds;
}
- (void)prepareForReuse
{
[super prepareForReuse];
}
#pragma mark - Private Methods
-(void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate index:(NSInteger)index andCellType: (BOOL) typeCell
{
self.collectionViewHorizontal.dataSource = dataSourceDelegate;
self.collectionViewHorizontal.delegate = dataSourceDelegate;
self.collectionViewHorizontal.tag = index;
self.cellType = typeCell;
[self.collectionViewHorizontal reloadData];
}
- (void) setFlowLayouts
{
_layout = [[UICollectionViewFlowLayout alloc] init];
_layout.sectionInset = UIEdgeInsetsMake(10, 20, 9, 10); //10, 20, 9, 10
_layout.itemSize = CGSizeMake(270, 148);
_layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionViewHorizontal = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
[self.collectionViewHorizontal registerClass:[ShowCollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier];
self.collectionViewHorizontal.backgroundColor = [UIColor clearColor];
self.collectionViewHorizontal.showsHorizontalScrollIndicator = NO;
[self.contentView addSubview:self.collectionViewHorizontal];
}
The cells in the collection view on the first table index has the billboard dimension type. While the remaining table indexes have collectionview cells with the portrait dimension type.
As the user scrolls the tableview up and down, on an iphone 5s and below, the initial table indexes display the billboard and portrait dimensions in their collectionviews correctly. However, towards the bottom the billboard collectionview cells get incorrectly used where portrait cells should be displayed as well UNTIL the user swipes left and right on that collectionview where it then corrects itself. The content in the cells are correct throughout the application.
Is there a way for me to force it to draw correctly as the user sees that collectionview for the first time?
Hopefully my post makes sense. I would be grateful if you could help! Thank you.