在UITableView的顶部添加UIView

时间:2016-10-24 10:07:10

标签: ios objective-c iphone uitableview uiview

我有UITableView并在其上方查看。有时我有很多内容所以这个标题应该向上滚动,当我向下滚动时必须立即显示(如Facebook应用程序)。我有一个刷新控制权,所以当我拉下来刷新我的标题时UIView应该高于UITableView(如果我使用UITableViewHeader它会向下移动UITableView)。你有什么建议吗?我真的被困了。提前谢谢。

2 个答案:

答案 0 :(得分:0)

#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) {
            }];
        }

    }


}

答案 1 :(得分:0)

我尝试了一点来实现你想要的东西。我想出了这段代码。但是你可能不得不自己做一些调整。我只是将sectionHeight变量设置为60并使用它将其作为节高度返回。请务必使用viewDidLoadviewWillAppear方法将此变量设置为60(或视图的高度)。并且return UIView要显示在UITableView viewForHeaderInSection方法中的#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; } 上方。如果您对代码有任何疑惑,请告诉我。

*** Keywords***