I have a custom UICollectionViewCell. After I return it from UICollectionView's datasource, what retains it?
The cell is added to the scroll view (= collection view). Views retain their subviews.
UICollectionView is the special scrollview which was inherited from UIScrollView.Refer this Documentation.
Which will have collection of UICollectionViewCell inside of it.
When you return it from cellForItemAtIndexPath as data source, it will be added (retained) to the UICollectionView by Apple's internal implementation.
So whenever you add some view to other view with addSubView: method , that will be added/retained by parent view.
Any View must have only one parent view. Not more than that.
Related
My self.view has a UIScrollView as a subview. This UIScrollView has a container UIView, which holds many UITableView's as subviews. These subviews are all configured using autolayout constraints.
So basically my scrollview is horizontally scrollable. I also have UILongPressGesture inside my UIScrollView.
My problem is didSelectItemAtIndexPath is not called while tapping over the cells inside UITableView. I have set cancelTouchesInView to NO for my UILongPressGesture but still didSelectItemAtIndexPath doesn't get called.
I don't want to add UITapGestureRecognizer to my UIScollView and manually trigger didSelectItemAtIndexPath , since I'm doing some operations in UITableView delegate methods.
How can I make my UITableView to responds to all of its delegate methods ?
I have a UITableViewController. In one of my prototype cells there is a UIScrollView with horizontal paging and a page control. I have set this up in storyboard, laying out all the elements that will display on each page of the UIScrollView, exactly how I have set a scroll view up in the past in a UIViewController. Since this UIScrollView is inside a UITableViewCell, I am confused about how to connect the page control.
1) Should the code to change the page control be in the cell class file? And if so what would that look like?
2) Also do I implement the UIScrollViewDelegate inside the UITableViewController or inside the UITableViewCell class file?
(this is in objective-c)
Having the UIPageControl code inside of the cell class seems to be the most appropriate choice. The view controller which is displaying the table/cells shouldn't be delegating this type of internal behaviour.
If you're implementing the code in the cell class then the cell class would need to conform to the UIScrollViewDelegate protocol in order to receive the scrollViewDidScroll: message. The table view controller wouldn't be involved.
This is what it the method would look like:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat pageIndex = self.scrollView.contentOffset.x / CGRectGetWidth(self.scrollView.frame);
self.pageControl.currentPage = lround(pageIndex);
}
Also make sure to set the pagingEnabled property on the scrollview to YES.
I put the iCarousel View in a table View and try to scroll the iCarousel, but the problem is it's not scrollable at all.
I can see iCarousel in the cell like following:
and I put iCarousel datasource and delegate in the tableView Controller, create a customer function to set the iCarousel datasource and delegate.
In the CustomTableViewCell I just define the setting delegate function:
- (void) setICarouselDataSourceDelegate: (id<iCarouselDelegate,iCarouselDataSource>)dataSourceDelegate {
self.carousel.dataSource = dataSourceDelegate;
self.carousel.delegate = dataSourceDelegate;
[self.carousel reloadData];
}
and in the MainTableView, I call the DataSourceDelegate in the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
First of all iCarousel is not any standarad View ... you should put link of the control that you're using
Second it seems like iCarousel is uses UIScrollView and UITableView is also derived from UIScrollView It's never a good idea to put a scrollview inside another scrollview.
This might be the reason why your table view is not scrolling.... BTW are you talking about horizontal scrollign or vertical scrolling ?
Currently I am using the iCarousel in Table View cell and it scrolls easily.
Just add standard view to cell in Storyboard.
In IB mark the class of that view to iCarousel.
Furthermore !ENABLE! user interaction on that view. (Maybe because of that you can't scroll the content inside the cell)
Implement separate datasource & delegate for every cell. (It is more clear which data are loading in the cell and also the cell itself can handle the data. You just pass the data to cell and it handles it accordingly.) - But it depends on you if you want to let one class to handle two separate control datasources & delegates :-)
I want to use
-preferredLayoutAttributesFittingAttributes inside the UICollectionviewCell subclass we are writing.
Currently we already had a weak pointer to out parentViewController and I try to set the collectionViewcell frame width to be the same as parentViewcontroller.view.frame.size.width.
I don't want to have a pointer to the parentViewController from the CollectionViewCell subclass as having a reference to the parentviewcontroller is a bad idea.
My question is....
Is there a simple and ideal way to get the frame of collectionviewcell's super view from inside the collectionViewcell class?
If you want to be use the parent view then you don't need any reference. You can do like this:
CGFloat width = self.superview.frame.size.width;
And as #Ralfonso pointed out in the comments:
Also, if you want to get the superview size immediately after being added to a view hierarchy, override didMoveToSuperview: and access the superview property. If it's nil, the view has been removed from the superview. If non-nil, the view has been added to the superview's hierarchy.
I'd like to know when a UICollectionViewCell is displayed on the actual screen. cellForRowAtIndexPath is insufficient, as the cell isn't actually seen at this point. didEndDisplayingCell is insufficient, as it gets called when the cell is removed from view.
UITableViewDelegate has a willDisplayCell method that I've found useful for similar stuff in the past, but it doesn't appear to exist in UICollectionViewDelegate.
How can I determine when the cell is brought on to the screen?
Make a subclass of UICollectionViewCell. Override its didMoveToWindow method:
- (void)didMoveToWindow {
if (self.window != nil) {
// I am now in the window's view hierarchy and thus “on screen”.
}
}
Technically, the cell could still be not visible, either because it's outside the window's visible bounds, or because it's covered by another view. But normally neither of those will be the case.
Note also that if a cell is reused, it might not get removed from and re-added to the view hierarchy. The collection view can just change the cell's frame. (I know UITableView does this with table view cells as of iOS 6.0.) In that case, you won't receive a didMoveToWindow message when the cell gets re-used for another item.
If you explain why you want to know when a cell is displayed, we might be able to give you a better answer.
-[UICollectionView visibleCells] returns a NSArray of all cells visible on screnn