Set up page control with uiscrollview that is inside uitableviewcell - ios

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.

Related

UIView not filling table views tableHeaderView property swift

I have a very peculiar situation and want to know why its happening. I have an edit box and button in a view that I put into a tableview's tableHeaderView property. I call this view InputToListCell.
Now when the view extends UIView I get the following:
i.e all I change in the code is:
class InputToListCell: UIView{
//Code Here
}
I then change InputToListCell to extend UITableViewCell and get the following:
i.e
class InputToListCell: UITableViewCell{
//Code Here
}
Why is this behaviour occurring? I cant see why because UITableViewCell extends UIView. Any thoughts?
Update:
Based on a comment made here are the constraints for the InputToListCell:
I basically pin both the edit text's and Add buttons constraints to the margin, except for the edit text's trailing value.
In short
Use UITableViewCell for table view elements.
self.tableView.tableHeaderView =
tableView.dequeueReusableCellWithIdentifier("sectionIdentifier")
Discussion
Using UITableViewCell objects for all table view elements not only provide an umbrella behavior when presented inside a UITableView, but will also allow loading from a .storyboard or .xib, an added bonus and cohesion.
Thus, even though tableHeaderView (and viewForHeaderInSection for that matter) is informally just a UIView, you can return a UITableViewCell. According to this post, to take advantage of Autolayout in tableHeaderView, you need to do a bit of gymnastics:
if let tableHeaderView = tableView.dequeueReusableCellWithIdentifier("tableHeaderViewIdentifier") {
tableHeaderView.setNeedsLayout()
tableHeaderView.layoutIfNeeded()
let height = tableHeaderView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
var frame = tableHeaderView.frame
frame.size.height = height
tableHeaderView.frame = frame
self.tableView.tableHeaderView = tableHeaderView
}
From a different post:
For a complete example on how to use .storyboard or .xib for headers, including an Xcode project, see https://stackoverflow.com/a/32261262/218152

iOS 7 - Layout advice

I have the following layout in my view controller. I want to be able to scroll vertically with the header scrolling off the view and the UISegmentedControl sticking to the top of the view, beyond that the remaining scroll should be handled by the Collection View.
However I'm a bit confused as to what is the best approach to implemented this layout.
I tried a few implementations with mixed results:
UIScrollView with UICollectionView as subviews: UIScrollView as the parent view with the header, segmented control and collection views as child controls. The problem with this approach is that the nested scrolling does not seem to work correctly. To be able to scroll the UIScrollView the tap needs to be outside the CollectionView area otherwise only the CollectionView scrolls and the header and segmented control don't move.
Header and Segmented Control in Header cell: I tried another approach by using a single CollectionView. I added the header and Segmented Control as subviews of a single Header cell of the collection view. When the segmented control value was changed, I switch the data source property of the CollectionView to achieve the 3 views required for the collection view. Visually everything works perfectly. The only problem here is the race condition when switching quickly between first,second and third tabs. I load the data from a web service, if the web service takes time and is still loading the data and I quickly switch the tabs then I run into bugs where the data returned is for a different collection view than what is currently selected, a lot of out of order sync issues.
Update constant value for Autolayout Constraint: Another approach I tried is to change the constant value of the auto layout constraint applied to "Header" view. Then I added a gesture to the view controller's view to track the scroll, as the user scrolls vertically I adjust the constant of the auto layout constraint so that the "header" cell pops out of view. Again this doesn't seem to work that smoothly, but I suppose I can tweak it, but it seems sort of a hack.
Is there a better way to implement this layout?
#2 seems like a good solution — the scrolling gestures will be most consistent with what users expect, since it's all a single scroll view. (I agree that #3 sounds like a hack.) You can make the header "sticky" with some custom layout attributes.
The only problem here is the race condition when switching quickly between first, second and third tabs.
This is a common problem with asynchronous loading when views are being switched out (especially when you are loading data into individual cells, which are being reused as you scroll). It is important that upon receiving the data you always check whether the receiver is still expecting it; i.e., you should check the segmented control value before changing the backing data source. You could also:
Use separate data source objects for the different segments, having each one manage its own data fetching so they can't get mixed up.
Cancel the outstanding requests, if you can, when quickly switching tabs, to avoid unnecessary network requests.
Cache data to avoid re-fetching every time you switch tabs.
I think you want the same functionality that pinterest profile page have. To implement such functionality at easy way, you need to do following things.
Step 1 : Add UIView as tableHeaderView those who showing off while scrolling up.
self.tableHeaderView = yourView
Step 2 : Add UISegmentControl in section header view.
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section{
return your_segmentcontrolView;
}
Step 3 : Add UICollectionView into first row of first section.
By implementing following way, you can got your desire functionality.
Hope this help you.
An alternative approach you could consider:
Use a UITableView to contain your UI
Create a UITableView, and set your header as the UITableView's headerView.
Use a sectionHeader to contain the segmentedControl.
Place your collectionView inside of a single UITableViewCell. Or alternatively, you may be able to use the UITableView's footerView to contain the gridView.
By using the sectionHeader, this should allow the header to scroll out of view, but then the sectionHeader will stick below the navigationBar or top of the contentView until another section comes into view (and in your case you will only have one section.)
Add Header View, Body View (Holding Segment View & Collection View) into scroll view.
Initially set userInteractionEnabled property to "NO" for collection view.
Track the insect of scroll view always.
If the y-coordinate of the scrolled insect is more than the height of header view, then set userInteractionEnabled property to "YES" so that thereafter collection view can be scrolled.
If user scroll outside the scroll view and try to bring the header view down, i.e Scroll view y-coordinate insect is less than the height of header view, then immediately change the user iteration mode of collection view and allow user to scroll the scroll view till the top.
Rather than implementing this by hand, you could use a library/cocoapod to set this up for you. This one looks like a pretty good fit: https://github.com/iosengineer/BMFloatingHeaderCollectionViewLayout
Plus, the code is open-source, so you can always modify as needed.
All I can say is that you need to subclass UIView and make it a delegate of UIGestureRecognizerDelegate and UICollectionViewDelegate, then in your UIView subclass, do the following, I can't give out anymore information on this because the code, although owned by myself, is proprietary to the point of probably enraging quite a few organizations that I've used this for, so here's the secret sauce:
CGPoint contentOffset = [scrollView contentOffset];
CGFloat newHeight = [_headerView maxHeight] - contentOffset.y;
CGRect frame = [_headerView frame];
if (newHeight > [_headerView maxHeight]) {
frame.origin.y = contentOffset.y;
frame.size.height = [_headerView maxHeight];
[_headerView setFrame:frame];
} else if (newHeight < [_headerView minHeight]) {
frame.origin.y = contentOffset.y;
frame.size.height = [_headerView minHeight];
[_headerView setFrame:frame];
} else {
frame.origin.y = contentOffset.y;
frame.size.height = newHeight;
[_headerView setFrame:frame];
}
if ([_delegate respondsToSelector:#selector(scrollViewDidScroll:)]) {
return [_delegate scrollViewDidScroll:scrollView];
}
You must subclass another UIView that is defined as the header for this custom UiCollectionView. Then, you must declare the UIView custom header view inside the custom subview of the UIView/UICollectionView delegate, and then set the header of that custom subview inside the UICollctionViewdelegate. You should then pull in this compounded subclass of UIView/UIcollectionView into your UIViewController. Oh yes, and in your layoutSubViews, make sure you do the height calculations that are passed through a double layered subclass. So, you will have the following files:
UIVew this is the delegate of UICollectionView and what I mentioned before
UIView this is a UISCrollViewDelegate and this is the header view
UIViewController that pulls in the subclassed UIView in number 1
UIView subclass of number 1 that pulls in number 2 and sets it as its header
In the number 4 part, make sure you do something like this:
- (CGFloat)maxHeight
{
if (SCREEN_WIDTH == 414)
{
return 260;
}else if (SCREEN_WIDTH == 375)
{
return 325;
}else
{
return 290;
}
}
- (CGFloat)minHeight
{
if (SCREEN_WIDTH == 414)
{
return 90;
}else if (SCREEN_WIDTH == 375)
{
return 325;
}else
{
return 290;
}
}
This will then pass through to the UIView subclass that is a compounded subclass as I already explained. The idea is to capture the maxHeight of you header in the subclass of this header UIView (number 2 above), and then pass this into the main UIView subclass that intercepts these values in the scrollViewDidScroll.
Last tidbit of information, make sure you set up your layoutSubviews in all methods to intercept scroll events. For example in number 1 above, the layoutsubviews method is this:
- (void)layoutSubviews
{
CGRect frame = [_headerView frame];
frame.size.width = [self frame].size.width;
[_headerView setFrame:frame];
[super layoutSubviews];
}
This is all I can give you, I wish I could post more, but this should give you an idea of how it's done in production environments for the big time apps you see out in the wild.
One more thing to note. When you start going down the road of intense implementations like this, don't be surprised to learn that, for example, a single view controller in an app that works with methods like I've explained will have anywhere from 30-40 custom subclasses that are either subclasses in their own right or compounded subclasses or subclasses of my own subclasses or my own subclasses. I'm telling you this so you get an idea of how much code is required to get this right, not to scare you, but to let you know that it might take a while to get right, and to not kick yourself in the butt if it takes awhile to make work. Good luck!!

Scroll second UITableView in line with second UITableView

I have two table views set up side by side, and I need them to scroll at exactly the same time. So, when you scroll one, the other one will scroll at the same time.
I did some searching and I couldn't find any information, but I assume it must be possible somehow.
My table views are both connected to the same class and I differentiate between them like this:
if tableView == tableView1 {
//
} else if tableView = tableView2 {
//
}
You can get set the scrollView delegate to self on both of your tableView's scrollViews. And in -scrollViewDidScroll, take the contentOffset and set the other scrollView's contentOffset to the same value.
Like Schemetrical said you should use scrollViewDidScroll.
see the first answer of this:
Scrolling two UITableViews together
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
UITableView *slaveTable = nil;
if (self.table1 == scrollView) {
slaveTable = self.table2;
} else if (self.table2 == scrollView) {
slaveTable = self.table1;
}
[slaveTable setContentOffset:scrollView.contentOffset];
}
If you want them to scroll in prefect lock-step then this isn't a trivial problem. UITableView is a subclass of UIScrollview, so you could probably create a custom subclass of UITableView that overrode various UIScrollView methods and when something caused the table view to scroll, it would do the same thing to the other table view.
Edit: #Schemetrical's suggestion of using the scroll view delegate is cleaner than creating subclasses. You might have to monitor quite a few of the scroll view delegate methods and use them to match the behavior in the other scroll view.
EDIT #2:
Apparently I'm wrong and scrollViewDidScroll is called for every change in the scroll view, so it's simpler than I thought to keep them synced. I'm going to leave my answer for context even though I was wrong.

How to make DPFBannerView in UITableview did not scroll

i am new in Google admob development. In this case i need to add an admob inside my uitableview, I make an implementation like this on :
the problem is, the cell that contain the ads (I Use DFPBannerView) is scrollable, i want to make it unscrollable. I just want to make the uitableview scrollable but the banner view is not. I already implement some suggestion from UIScrollView doesn't scroll when touching GADBannerView subview
but it still can not make the ads unscrollable. is there anyone can help me?
There are basic 2 solutions to this:
Add the view to the superview of your table view instead of to the table view itself. Then you just set the frame of the tableview to be shorter so that it is not cut off by the banner. This is provided you are not using a UITableViewController because if you're using one of those, the table view is the root view.
Add the subview to your tableview directly, and implement scrollViewDidScroll: and change its frame based on that. The implementation would look something like this:
- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect bannerFrame = myBanner.frame;
bannerFrame.origin.y = CGRectGetMaxY(scrollView.bounds) - bannerFrame.size.height;
myBanner.frame = bannerFrame;
}

Make UIView under a UITableView visible when you scroll

I have a UITableView that's placed on top of a UIView.
So basically, it looks like this:
I have a tableviewHolder which has two subviews
1.) UIView with other subviews
2.) UITableView
Whenever the user scrolls the UITableView, i want the UIView beneath it to be visible.
I tried setting the UITableView's background to clear and it works. However, if the tableview's background is clear, then other portion of the UIView beneath it would be visible when the rows of the cell are not enough to cover the whole table.
How can I change the background color of the table view and still make the UIView beneath it visible whenever the user scrolls?
Use
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
(ref https://developer.apple.com/library/ios/documentation/uikit/reference/UIScrollViewDelegate_Protocol/Reference/UIScrollViewDelegate.html#//apple_ref/occ/intfm/UIScrollViewDelegate/scrollViewWillBeginDragging: )
This method will be called when the UITableView begins scrolling. So, in this method, set the UIView to visible
view.setAlpha=1.0;
and in
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
set the uIView to invisible again by using
view.setAlpha=0.0;
Am asking you to use UIScrollViewDelegate methods in a UITableViewDelegate class. This will work because UITableViewDelegate inherits from UIScrollViewDelegate. So all methods in UIScrollViewDelegate will also work for UITableViewDelegate.

Resources