In my app, I have a UICollectionView inside a UITableView that is showing cells to select a day.
I had a weird issue where sometimes, the cells would be centered correctly, and sometimes it wouldn't, to the point where part of the first cell is cut off in the view.
Normal
Not Aligned
What is going on? I've adjusted the UIEdgeInsets, but it doesn't seem to make an effect.
I have found out that my issue was fixed when I removed this line of code for the UICollectionViewFlowLayout of the UICollectionView
For a defined
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
Remove this
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
Related
In my project, I have a UITableView with different types of cells.
One of these cells contains a horizontally scrolling UICollectionView.
When I run my project, the UICollectionView is correctly populated, but I cannot scroll horizontally, clicking and dragging inside this UICollectionView only allows for me to scroll the tableview vertically.
The desired behaviour would be that a click inside the cell with the UICollectionView would allow the user to scroll horizontally, clicking in any other cell allows for vertical scrolling.
I'm assuming that I need to overwrite/pass on or disable some OnClick event in this cell however I cannot find the correct solution.
Any help pointing me in the good direction would be greatly appreciated :)
The code for creating the concerning TableViewCell looks like this.
#property(nonatomic) HorizontalCollectionViewDelegate *horizontalCollectionViewDelegate;
- (UITableViewCell *) createHorizontalScrollCell:(NSIndexPath *)indexPath tableView:(UITableView *)tableView{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCellIdentifier];
MyHorizontalScrollingTableCell *myHorizontalScrollingTableCell = (MyHorizontalScrollingTableCell *)cell;
UICollectionView *horizontalCollectionView = myHorizontalScrollingTableCell.myHorizontalCollectionView;
//Link collectionView to its delegate
self.horizontalCollectionViewDelegate = [[HorizontalCollectionViewDelegate alloc] init];
horizontalCollectionView.delegate = _horizontalCollectionViewDelegate;
horizontalCollectionView.dataSource = _horizontalCollectionViewDelegate;
_horizontalCollectionViewDelegate.data = myData;
//Define the cells in HorizontalCollectionView
UINib *cellNib = [UINib nibWithNibName:#"CollectionViewCell" bundle:nil];
[horizontalCollectionView registerNib:cellNib forCellWithReuseIdentifier:#"horizCell"];
//Define flow of AttachmentsCollectionView
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(90, 90)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[horizontalCollectionView setCollectionViewLayout:flowLayout];
return cell;
}
EDIT: I'm using the same HorizontalCollectionViewDelegate elsewhere in my project (just not in a tableview) and over there it is working, so I assume the problem is not there.
EDIT 2: Problem solved, turns out there wasn't a problem (see answer below), will be accepting my answer in two days when I can.
Solved...
Turned out I only needed to add more content to the UICollectionView.
With 2 or 3 elements the Cells did not fill the complete view. Because the swipe animation was not showing I assumed there was an error somewhere.
However just adding more content solved this issue.
I use a dynamic tableViewController. I implement the numberOfSection to return 4, and the numberOfRowInSection return 1. These are the only changes I made for the blank TableViewController. But when I run it on simulator, it shows multiple cells, which I expected to be only four cells. Why is that?
Note: If I change the height of each cell to let the screen only fit four cells at a time, it will work fine since it won't let be me scroll down. So I guess is the tableViewController always try to fill the screen no matter how many cell it should display?
Yes, the UITableView will always continue to fill the screen with 'empty' cells of the default row height. To prevent this, you could add the following code in viewDidLoad:
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
(For Swift, check #VictorSigler's answer.)
In Swift:
self.tableView.tableFooterView = UIView(frame: CGRect.zeroRect)
I have a UICollectionViewCell subclass that I have defined in a nib...
It uses AutoLayout to layout all of the (two, lol) components. The date picker is laid out with left, right and bottom constraints to the superview and a height constraint to make sure it always shows full content.
Then, when configuring the layout of the collection view I use this...
_layout = [[UICollectionViewFlowLayout alloc] init];
_layout.itemSize = CGSizeMake(CGRectGetWidth(self.collectionView.frame), CGRectGetHeight(self.collectionView.frame));
_layout.minimumInteritemSpacing = 0;
_layout.minimumLineSpacing = 0;
This works as when I'm debugging the view I can see that the frame of the UICollectionViewCell is correct, but the content of the cell is not resizing as you can see from these two screenshots.
Collection View Cell selected and is correct.
Content of collection view selected and is not correct.
In fact, you can see that the content is the same size and shape as defined in the nib. But
this should resize to match the cell size. If I make the nib content really small it stays really small in the app.
This is, potentially, happening in UITableViewCells too as the cell separators are not showing all the way across the screen for bigger devices.
I am using collectionView in my iPad application. I have used custom layout class which inherited UICollectionViewFlowLayout class.
I am using horizontal scroll directional. The problem is, whenever we use scroll for collection view, some times cell get disappear. I debugged the code, and found that in data source method after creating cell, its hidden property get automatically ON.
I tried using below line but its not working
cell.hidden = NO
I have below method also to invalidate layout on bound change.
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return YES;
}
But still I am facing problem. Any solution on this ?
Thanks in advance.
There is a known issue which may cause cells to disappear in UICollectionView. If their fix doesn't work for you, try PSTCollectionView,
which is an open source replacement for UICollectionView. If PSTCollectionView works, you should file a radar ticket to Apple.
In my project, the UICollectionViewCell disappear also ,the flowLayout i use is a custom layout named UICollectionViewLeftAlignedLayout . Its scroll direction is horizontal .I debug the project again and again, have tried every solution i can find through google.com. All that did not work for me.
At the end ,i delete the UICollectionViewLeftAlignedLayout and use native
UICollectionViewFlowLayout through:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_collectionView.collectionViewLayout = layout;
_CollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
And I set the cell spacing through storyboard(also you can set them through the layout's propery):
Then i run the project again , the UICollectionViewCell DO NOT DISAPPEAR anymore. Hope my case can help you .
I have a grouped UITableView with custom cells loaded from nib files. They have the proper 300 px width (for grouped) and appear okay, except for one problem: square corners. The first and the last cell of each group is not appearing to have rounded corners as it should. I have tried setting the backgroundcolor of the tableview to clear, as well as the background of the view. I initialize it in a pretty standard way in my UITableViewController with
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
I put this in the ViewWillAppear method. I've also tried it in the viewDidLoad method and it made no difference. Any help on getting these corners rounded would be much appreciated! Thanks!