在UITableView上方添加隐藏视图,该视图仅在向上滚动TableView时显示

时间:2012-06-18 03:14:45

标签: ios objective-c ios5

目标/情况:
我目前在TableView标题中有一个UIView。我试图添加另一个UIView(包含两个按钮和一些TextFields),它们将位于TableView标题之上。我希望当用户向上滚过标题时显示视图(la“刷新”),并在用户按下视图上的“完成”按钮时离开。

我的三个问题:

  
    
        
  1. 如何在tableview标题上方添加视图?
  2.     
  3. 我该怎么做     当用户向上滚过标题时显示所述视图?
  4.     
  5. 当用户按下按钮时,如何解除所述视图     视图?
  6.        

编辑:
我将使用@ kimpoy的建议将我的自定义视图添加到第三方PullToRefresh TableViewController。

4 个答案:

答案 0 :(得分:1)

如果我理解正确,你想做 用户触发的拉动动作只能触发两个不同的方法调用取决于用户拉多少。

我假设你要添加的UiView是“2nd Header”。 它应该类似于“pull to refresh header”。

我认为魔术也在利用UIScrollView委托。 在许多示例中,您可以看到“拉到...”只需检查scrollView.contentOffset.y

所以,你可能会这样:

//用于检查拉动刷新功能的代码(简化)

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
      if (self.scrollView.contentOffset.y <= - 65.0f) {}
  }

- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
      if (self.scrollView.contentOffset.y <= - 65.0f) {}
  }

将其更改为:

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
     if ( -65.0f <= self.scrollView.contentOffset.y <= - 55.0f  ) {
         // give a area for checking the origin pull to refresh action
     }

     if (self.scrollView.contentOffset.y <= - 65.0f) {
        // checking for ur function
     }
 }

 - (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
     if ( -65.0f <= self.scrollView.contentOffset.y <= - 55.0f ) {
        // give a area for checking the origin pull to refresh action
     }

     if (self.scrollView.contentOffset.y <= - 65.0f) {
        // checking for ur function

        // add ur view the UIScrollView / TableView
        // set ur scrollview offset to show ur whole form
        [scrollView setContentOffset:CGPointMake(0, y)];
     }

  }

最后添加一个方法调用ur表单按钮来做你想要的(删除表单并将scrollView内容偏移设置为(0,0))

(对不起,我是juz谈论的不是概念,我不确定它是否有效)

答案 1 :(得分:0)

http://three20.info/ 这个框架对你有很大的帮助。它有完全像你想要的工作表。并且“拉到刷新”区域是可以通行的

答案 2 :(得分:0)

你应该像这样实现它。使用EGORefreshTableHeaderViewPullToRefresh等第三方。

  1. 这两个可以帮助您在tableview上方创建“pull to refresh”视图。
  2. 在.h文件中声明第三方类的实例
  3. 将这两者中的任何一个实例化到要实现它的类。然后,要在“提取到刷新”视图中设置自定义外观,只需添加自定义视图。
  4. 例如:     // .h文件中的实例     EGoRefreshTableHeaderView * refresh;

    // In your .m file
    
    // Set up your custom view with the added buttons and textfields.
    UIView *customView...
    
    // Instantiate pull to refresh third party class and add to table
    EGoRefreshTableHeaderView *tempRefresh = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.tableViewCompanyContacts.bounds.size.height, self.view.frame.size.width, self.tableViewCompanyContacts.bounds.size.height)];
    [tempRefresh setDelegate:self];
    
    // Add your custom view
    [tempRefresh addSubView:customView];
    
    // Set your instance
    [self setRefresh:tempRefresh];
    
    // Release temp instance
    [tempRefresh release];
    
    // Add your third party to the table
    [self.tableView addSubView:refresh];
    
    // Implement delegate method (to refresh)
    

    通常情况下,此视图会在刷新后消失或隐藏。只需在代码中添加一些调整即可让用户在隐藏视图时进行处理(例如,单击按钮)。

    提供了“拉动刷新”动作的方法。告诉我,如果你发现了令人不安的事情。祝你好运!

答案 3 :(得分:0)

在viewDidLoad

中编写此代码
tempView = [[UIView alloc] initWithFrame:CGRectMake(7,81,305,40)];  
lbl = [[UILabel alloc] initWithFrame:CGRectMake(40,125,250,20)];  
lbl.text = @"";  
lbl.font = [UIFont fontWithName:@"Haveltica" size:14];  
lbl.textAlignment = UITextAlignmentCenter;  
lbl.textColor = [UIColor whiteColor];  
lbl.backgroundColor = [UIColor clearColor];  
[tempView setBackgroundColor:[UIColor colorWithRed:204/255.0 green:204/255.0 blue:204/255.0 alpha:0]];  
[tempView addSubview:lbl];  
[self.view addSubview:tempView];  
isReload=YES;    


-(void)viewWillAppear:(BOOL)animated  
{

     [super viewWillAppear:animated]; 

    if(isReload==YES)
    {
        // your code;  
    }
    isReload=NO;

}   

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate  
{ 

    NSLog(@"table view height is %f",self.tblview.frame.size.height);  
    NSLog(@"scrollview oringin %f",scrollView.frame.size.height);  
    NSLog(@"Top10VC: ScrollView did end dragging");  
    if (!decelerate)   
    {  
        //[self loadImagesForOnscreenRows];  
    }  
    if(scrollView.contentOffset.y<-60)  
    {  
    [self.spinner startAnimating];  
    [self performSelector:@selector(getdata1)withObject:nil afterDelay:0.2];  

    }  

}  

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView  
{

    NSLog(@"scrollview oringin %f",scrollView.frame.size.height);  
    NSLog(@"Top10VC: ScrollView did end decelerating");  
    //[self loadImagesForOnscreenRows];  
}  

-(void) scrollViewDidScroll:(UIScrollView *)scrollView  
{ 

    tempView.frame = CGRectMake(0,-80 - scrollView.contentOffset.y , 320,80);   
    if(scrollView.contentOffset.y < -60)  
    {         
        lbl.text = @"Updating...";  
                [scrollView setContentOffset:CGPointMake(0, -100)];
    }  
    else  
    {  
        lbl.text = @"";  
    }   
}