I have UICollectionView inside UITableViewCell, cells are of dynamic width depending upon text.The problem I am facing is when i scroll the TableView after that collection inside cell is appearing correct.
Here is what I did, I called reloadData after datasource is set and also called invalidateLayout but not working
Here is what I did,
Inside tableViewCell class,
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize calCulateSizze =[[datasource objectAtIndex:indexPath.row] sizeWithAttributes:NULL];
NSLog(#"%f %f",calCulateSizze.height, calCulateSizze.width);
calCulateSizze.width = calCulateSizze.width+20;
calCulateSizze.height = 35;
return calCulateSizze;
}
#pragma mark - Public
- (void)setCellData:(id)pData {
GSHColumns *column = (GSHColumns*)pData;
if (column.options.count > 0) {
datasource = column.options;
[_collectionView reloadData];
[_collectionView.collectionViewLayout invalidateLayout];
}
[titleLabel setText:column.displayName];
}
Here is what it looks like before scrolling
and after scrolling,
Related
I have a UICollectionView inside a scrollview which has cell with dynamic width and static height.
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return tagArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
TagsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"tagsCollectionViewCell" forIndexPath:indexPath ];
[cell bindWithModel:tagArray[indexPath.row]];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize yourLabelSize = [tagArray[indexPath.row] sizeWithAttributes:#{NSFontAttributeName : [UIFont systemFontOfSize:17.0f]}];
return CGSizeMake(yourLabelSize.width+20+40, 40);
}
I could calculate height using :
CGFloat height = self.collectionView.collectionViewLayout.collectionViewContentSize.height;
I am getting higher value than actual height.
collectionView:layout:sizeForItemAtIndexPath:
Asks the delegate for the size of the specified item’s cell.
CGFloat height = self.collectionView.collectionViewLayout.collectionViewContentSize.height
This will return the collectionView height not the collectionView Cell's height.
I am use ScrollView . In my scroll view there is Collection View. My collection view last 3 element hide inside white space. On scrolling ScrollView they not show.They Shown on scrolling CollectionView. I want to show shown all Collection Cell On Scroll ScrollView. Not Scrolling CollectionView.
-(NSInteger)numberOfSectionsInCollectionView:
(UICollectionView *)collectionView {
return 1; // The number of sections we want
}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section {
return 5; // the number of cells we want
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionViewd
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionCell* cell =
[collectionViewd dequeueReusableCellWithReuseIdentifier:#"collectionView2CellIdentifier"
forIndexPath:indexPath]; // Create the cell from the storyboard cell
cell.layer.cornerRadius = 4;
cell.layer.borderWidth = 1;
cell.layer.borderColor = [UIColor grayColor].CGColor;
cell.yellow.layer.cornerRadius=4;
cell.red.layer.cornerRadius=4;
cell.title.text =[titleArray objectAtIndex:indexPath.row];
cell.date.text =[dateArray objectAtIndex:indexPath.row];
cell.incomeText.text =[netIncomeArray objectAtIndex:indexPath.row];
cell.expenseText.text =[netExpenseArray objectAtIndex:indexPath.row];
cell.saving.text =[savingArray objectAtIndex:indexPath.row];
cell.expensePecentageText.text =[expensePercentage objectAtIndex:indexPath.row];
cell.savingPercentage.text =[netProfitPercentage objectAtIndex:indexPath.row];
return cell; // Return the cell
}
- (CGFloat)collectionView:(UICollectionView *) collectionView
layout:(UICollectionViewLayout *) collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger) section {
return 2.0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(2, 2, 2, 2);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(155, 158);
}
[scrollView setContentSize:CGSizeMake(0, expenseButton.frame.origin.y+expenseButton.frame.size.height+collection.frame.size.height+16)];
First of all count the height of collectionview By :
For Example
titleArray.count * 160 (158px cell height + 2px cell space)
Then assign this height to collectionview frame
also assign this to scrollview content size height.
This will surely do the trick for you.
I am wondering if there is a way to scroll a certain UICollectionViewCell to the top of the view?
I tried the collectionView.scrollToItemAtIndexPath() method, but instead of scrolling the cell to the top of the view, it scroll the cell to the center of the view.
You may use below code:
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
}
However, if you want to force the scrolling, then try this.
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
[collectionView setContentOffset:CGPointMake(cell.center.x - collectionView.frame.size.width * 0.5, cell.frame.origin.y) animated:YES];
}
You can also implement the UIScrollViewDelegate method scrollViewShouldScrollToTop: and return YES if the passed in scroll view is equal to the one that you want to scroll to the top:
-(BOOL) scrollViewShouldScrollToTop:(UIScrollView*) scrollView
{
if (scrollView == self.myTableView)
{
return YES;
}
else
{
return NO;
}
}
if you don't have more than one scroll view/table view/collection view on screen
I am stuck in frame of uicollectionview cell frame
what I want to do is,
in landscape mode of iPad I want to show 3 cells,
and in portrait 2 cells.
when I write the code of frame in method - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
its not affecting the size of frame.
I checked by applying debugger whether its coming in this method or not.
but its coming and returning the size.
by default I have to launch my application in landscape and then if required in portrait.
Here is how its showing in landscape
Here how this cells looking in portrait
Here is my code for frame
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//return [self.cellSizes[indexPath.item] CGSizeValue];
if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
{
return CGSizeMake(100, 100);
}
return CGSizeMake(200, 200);
}
What ever value I pass, frame is not at all changing.
I didn't passed any other frame value any where.
I want to give hardcoded value to cell frame first then will go for dynamic.
but I'm stuck at the first place only.
Can any one please help me.
Thanks in advance.
i faced similiar problem so basically you should implement UICollectionViewDelegateFlowLayout in your view controller otherwise design a custom collectionviewcell and register in your view controller , it will definitely work . Try it .
in my case
i added xib by subclassing UICollectionViewCell and designed it accordingly and it works so you may try .
For dynamic collection view cell you can use collection view methods like this.
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section{
return [_elements count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"cell";
MosaicCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
MosaicData *data = [_elements objectAtIndex:indexPath.row];
cell.mosaicData = data;
float randomWhite = (arc4random() % 40 + 10) / 255.0;
cell.backgroundColor = [UIColor colorWithWhite:randomWhite alpha:1];
return cell;
}
For details you can see this example. This is an perfect example of your need
https://github.com/betzerra/MosaicLayout
You need to reload the collection view when orientation will get change. so the CGRect size method will be called for the cell to recreate cell.
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// do something after rotation
[_collectView reloadData];
}
Add this method and reload your collection view so - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath will get called and size will get changed.
Thanks
i want to change the size of cells in a uicollectionview with a slider but i cant find how to do.
its like in the macOs finder when you see icons, you can change the size of it with a slider.
I want it for an iPad app
- (IBAction)sliderChanged:(id)sender {
[self.tableView reloadData];
}
Hook up your slider to the above method (for value changed), then use the following method to define the height:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:
(NSIndexPath *)indexPath {
return self.slider.value;
}
Nearly perfect, i want for a uicollection view so i need to change with:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//Return the size of each cell to draw
CGSize cellSize = (CGSize) { .width = 320, .height = self.slider.value};
return cellSize;
}