2010-02-09 69 views
14

还有一个类似的问题,但答案非常笼统,模糊。(Detecting UITableView scrolling) 请不要放弃。我正在寻找具体的解决方案。如何检测UITableView的滚动?

我有UITableView其中有可编辑的文本字段,并选择其他单元格时出现PickerView。 我需要的是当用户开始滚动这个UITableView时隐藏firstResponder或PickerView。

到目前为止问题Detecting UITableView scrolling有一个消歧,你应该子类UITableView。如果你的子类UITableView仍然是内部/私人UIScrollView不可访问。 如何访问UITableView的父级ScrollView(不违法)?

谢谢。

回答

34

您不需要继承子类UITableView来跟踪滚动。您的UITableViewDelegate也可以作为UIScrollViewDelegate。因此,在您的代表课程中,您可以实施-scrollViewWillBeginDragging:或您所需要的任何UIScrollViewDelegate方法。 (如问题实际上建议你提到)

+1

谢谢,我明白了。理由添加到 @interface MyTableViewController:UIViewController 然后实现-scrollViewWillBeginDragging: 谢谢。 – Rod 2010-02-09 07:51:43

+0

如何才能从特定的表中滚动?举例来说,我有2个UITableViews? – 2014-04-24 06:01:04

+1

@jeraldo,所有UIScrollViewDelegate方法都接受将其作为参数调用的滚动视图,因此您可以轻松确定要处理的是哪个UIScrollView – Vladimir 2014-04-24 06:57:30

8

要在普京的回答扩大,这是我如何实现这个解决方案:

在.h文件中:

@interface MyViewController : UIViewController <UIScrollViewDelegate> 

在.m文件:

- (void)scrollViewWillBeginDragging:(UIScrollView *)activeScrollView { 
    //logic here 
} 
1

我有同样的问题,我从上面的答案中得到了一些想法来解决它,但不仅如果我想在表视图滚动时刷新应用程序崩溃,而且它也cr如果我在刷新时滚动,就会灰飞烟灭。因此,解决所有情况下的问题的扩展解决方案是:


1.1。如果用户按下刷新按钮,则禁用滚动功能

1.2。刷新过程完成后启用滚动功能


2.1。如果用户正在滚动,请禁用刷新按钮

2.2。使刷新按钮一旦用户完成滚动


为了实现第一部分(1.1和1.2。):

-(void)startReloading:(id)sender 
{ 
    ... 
    self.tableView.userInteractionEnabled = NO; 
    // and the rest of the method implementation 
} 

-(void)stopReloading:(id)sender 
{ 
    self.tableView.userInteractionEnabled = YES; 
    // and the rest of the method implementation 
} 

为了实现第二部分(2.1和2.2。 ):

- (void)scrollViewWillBeginDragging:(UIScrollView *)activeScrollView 
{ 
    barButton.enabled = NO; 
} 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
    barButton.enabled = YES; 
} 

而且因为它是在以前的答案解释,UISCrollViewDelegate需要在.h文件中设置:

@interface MyTableViewController : UITableViewController <UIScrollViewDelegate> 

P.S.您可以使用scrollEnabled而不是userInteractionEnabled,但这一切取决于您在做什么,但userInteraction是首选选项。

6
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { 
    if (scrollView == myTableView){ 
     // Your code here..... 
    } 
}