I have a UITableView
and view above it. Sometimes I have a lot of content so this header should be scrolled up and have to be immediatelly shown when I scroll down (like Facebook app). And I have a pull to refresh control so when I pulled down to pull to refresh my header UIView
should be above UITableView
(if I use a UITableViewHeader
it moves down with UITableView). What can you advice? I've really stucked. Thanks in advance.
#pragama mark delegate UIScrollView
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self scrollViewAnimation:scrollView];
}
-(void)scrollViewAnimation:(UIScrollView *)scrollView{
if (scrollView==YourtableViewName) {
[YourtableViewName layoutIfNeeded];
if ([scrollView.panGestureRecognizer translationInView:scrollView.superview].y > 0) {
NSLog(@"Scrolling Up");
[UIView animateWithDuration:0.25 animations:^{
viewTop_topConstraint.constant = 0;
[self.view updateConstraintsIfNeeded];
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
}];
} else if ([scrollView.panGestureRecognizer translationInView:scrollView.superview].y < 0) {
NSLog(@"Scrolling Down");
if (dataArray.count>4) {
[UIView animateWithDuration:0.25 animations:^{
viewTop_topConstraint.constant = -120;
[self.view updateConstraintsIfNeeded];
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
}];
}
}
}
I tried a little to implement what you wanted. And I came up with this piece of code. But you might have to do a few tweaks yourself. I just set sectionHeight
variable to 60 and used that to return that as section height. Make sure you set this variable to 60 (or the height of your view) in viewDidLoad
or viewWillAppear
method. And return
the UIView
you want to show above your UITableView
in viewForHeaderInSection
method. Let me know if you have any confusions regarding the code.
#pragma mark - TableView Data Source Events
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"DeliveryPackageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = @"1";
return cell;
}
#pragma mark - TableView Delegate Events
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,sectionHeight)];
[view setBackgroundColor:[UIColor lightGrayColor]];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return sectionHeight;
}
#pragma mark - Srcoll View Delegates
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
self.initialContentOffset = scrollView.contentOffset.y;
self.previousContentDelta = 0.f;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat prevDelta = self.previousContentDelta;
CGFloat delta = scrollView.contentOffset.y - self.initialContentOffset;
if (delta > 0.f && prevDelta <= 0.f) {
// started scrolling positively
if(sectionHeight != 0)
{
sectionHeight = 0;
[tableView reloadData];
}
} else if (delta < 0.f && prevDelta >= 0.f) {
// started scrolling negatively
if(sectionHeight == 0)
{
sectionHeight = 60;
[tableView reloadData];
}
}
self.previousContentDelta = delta;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.