I am using a UICollectionView to show an array of images, horizontally and zoom in/out that image in the cell with the help of scroll view using constraints. But when I scroll horizontally it shows me previous cell content in the current cell. I am and not able to identify what is the problem.
CollectionView datasources
CollectionCell Class
try this when you reuse the Cell in cellForItem at
cell.imageView.Image = nil
Please share some code so that we can help you better. Meanwhile, make sure you have the following parts working correctly in your code:
Register correct UICollectionViewCell subclass with correct reuse identifier
Use the method [self.collectionView dequeueReusableCellWithReuseIdentifier:itemIdentifier forIndexPath:indexPath] and pass both the parameters correctly.
In UICollectionViewCell subclass, override prepareForReuse and reset the UI parameters there.
Override prepareForReuse in imageCell class, and set imageView.image to nil.
The contentSize of the scrollView should be as big as imageView.image.size, or just use MWPhotoBrowser
Related
I have used this tutorial to put a collectionView inside a UITableView. https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/
The UITableViewController is the dataSource & delegate for both the UITableView & the different collection views in each cell.
The problem is that I want to dynamically hide the CollectionView and to change its height to 0 whenever the collectionview is empty.
To do so, I have this code in CellForRowAtIndexPath
if (patients.paraclinicImage.count == 0){
[cell.collectionView setHidden:true];
cell.collectionViewHeight.constant = 0;
} else {
[cell.collectionView setHidden:false];
cell.collectionViewHeight.constant = 80;
}
By having this code, the collectionView shows up correctly initially. However, when scrolling fast, I will sometime have the cell load with the correct cell height, however the collection view will be empty. Refreshing the cell fixes this issue. Removing the above line of codes also fixes the issue.
Here are two images showing how the cell looks when first rendered, and after multiple scrollings (and re-renderings of the cell).
Before scrolling issue looks like below image
But After scrolling issue seems to like below one
I would appreciate any ideas you guys might have.
Debug view hierarchy showing an empty collectionView with an appropriate cell height
It is because your cell height is not updated on fast scrolling. UITableView caches the row height of indexPath just to calculate the scroll area. You need to set constraints properly and adjust the height of cell in heightForRowAtIndexPath: method.
Apply the logic inside heightForRowAtIndexPath: and your problem will be solved.
Happy Coding!!
I had the same issue. Sometimes, my collection view inside a tableviewcell wouldn't show the collectionview cells. I solved it by calling collectionView reload on main thread. I don't exactly know the reason, as I have many background calls on each cell and was reloading the tableview on main thread itself. Still, had to reload collectionview on main thread.
DispatchQueue.main.async {
cell.colViewProperties.reloadData()
}
I am not like to get the cell size inside cell for row at indexpath
Please anybody help me to get the cell size.
UICollectionView IBOutlet name is collectionData;
UICollectionView is data driven, like all iOS GUI classes. Which means - you do not ask the collection view for the cell size but you tell the collection view the cell size. So, where did you tell the collection view the cell size? In the storyboard, programatically in the layout? Or in a delegate func? There you have it - your cell size.
Am using custom tableview cell in my code. Please can anybody help me how to write the code for common friends cell. How can i get the names AMANDA,VIKA LEVINA,NATASHA,KATE,JESICA names in rectangular box.
For common friends and Facebook interest cells you can use UICollectionView and adjust cell width depends upon text otherwise you can use one of below listed controls.
https://www.cocoacontrols.com/controls/jctaglistview
https://www.cocoacontrols.com/controls/taglistview
https://www.cocoacontrols.com/controls/antagsview
you have to design two custom cells in your IB first for image view cell and other for list given below in your cell. create two custom cell in IB and design them according to your need and after that in your cell for row at index path method differentiate between these cell like:
uitablviewCell *cell;
if(indexPath.row == 0)
{
cell = [tableview dequereusablecellWithIdentifier:Firstcell];
}
else
{
cell = [tableview dequereusablecellWithIdentifier:secondCell];
}
For common friends and Facebook interest cells, You can use ANTagsView.
Using this, You can get tag view which you are showing in image.
So this view, you can add into your cell. For tags view, you have to pass array according to your view.
And set height of cell dynamically using heightForRowAtIndexPath method.
After that, you can get your view.
I am trying to get the height and some other attributes from a prototype UICollectionViewCell in my storyboard. I need those values in the ViewDidLoad, so before any cell is rendered.
I found this answer for tableview:
https://stackoverflow.com/a/14127936/2637259
But it doesn't seem to work for a collectionview because I need a indexpath.
I also found this answer which seems to solve the problem: https://stackoverflow.com/a/22761931/2637259
But I don't know how to do that in xamarin.
Can anyone enlighten me please?
Edit: The reason I need to have it at ViewDidLoad (or at least anywhere before the cells are loaded) is a UIWebView I have in the cell. I have a html string to load into the cell. So I made a function to calculate the height of the webview. After all cells are calculated I call CollectionView.ReloadData() to load the cells. In the sizeForItemAtIndexPath I need to check if the height of the cell is less than the height of the cell without the webview plus the calculated height of the webview. If not, I need to return the standard cell size. Else I need to return the cell height without the webview plus the calculated height.
You could just create static properties for you custom cell classes like this:
public partial class CustomCell: UICollectionViewCell
{
public static new float CellHeight { get { return 44f; } }
... rest of your class
then you can get the height by just calling
CustomCell.CellHeight;
Hope that helps! Cheers!
I'm trying to get a UIScrollView to work correctly inside of a UICollectionViewCell.
The custom cell is being loaded in via a xib file and is a subclass of UICollectionViewCell. I had problems getting other controls working, such as a button and a gesture recognizer since the UICollecitonView doesn't seem to be passing any touches to the cells, but I got around those with gesture recognizers on the UICollectionView itself. The one remaining issue I have is the UIScrollViews...
The UICollectionView scrolls horizontally, and the UIScrollView in the cells scroll vertically. I've tried using a UIPanGestureRecognizer to scroll them, but that seems to disable the UICollectionView's ability to scroll.
Anybody have any thoughts?
EDIT: Got it!
So I had converted to a collection view from a previous third party library we were using before iOS6. Turns out the problem was with the xib files we were using for the cells. With the library before, the cells were just subclasses of UIViews. I changed the classes to subclass UICollectionViewCell, and updated the Custom Class. Turns out this was not enough. In order for touches to get passed to the cells I needed to actually had to drag in a new UICollectionViewCell from the Object library, copy over all the subviews and reconnect the IBOutlets. After this, it worked!
I fixed this in my code by making sure resizing of the scroll view happens on main thread.
My collection view is using nsfetchedresultscontroller that is using block calls to refresh selected cells. On the first time a cell was selected the scrollview would not scroll. However if you clicked on another cell and clicked back it would work fine. The initial load of the cell seemed like size calc might not be where it needed to be on main thread to affect behavior.
-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"CustomCell" forIndexPath:indexPath];
dispatch_async(dispatch_get_main_queue(), ^{
// resizeScrollViewMethod should be where scrollview content size > scroll view frame.
[cell resizeScrollViewMethod];
});
return cell;
}
- (void) resizeScrollViewMethod {
//Do your scrollview size calculation here
}
No problem with UISCrollView in UICollectionViewCell. Use StoryBoard and you'll scroll OK.
UIScrollView overlay UICollectionViewCell, so that didSelect work only when tap outside ScrollView and inside Cell (scrollView.frame < cell.frame).
If you want to implement tap or other gesture, just add it to UIScrollView in awakeFromNib
Refer code:
https://github.com/lequysang/github_zip/blob/master/CollectionViewWithCellScrollViewStoryBoard.zip