首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIScrollViewDelegate方法中的重复代码

UIScrollViewDelegate方法中的重复代码
EN

Stack Overflow用户
提问于 2015-08-09 10:29:22
回答 1查看 65关注 0票数 0

我最近实现了一个解决方案,允许我知道滚动视图何时完成滚动。这样,当我滚动我的表视图时,我只会在tableview完全停止移动时调用一个特定的方法。

我遵循了这里提供的答案:https://stackoverflow.com/a/8010066/2126233,但am包括下面的代码以便于阅读:

代码语言:javascript
复制
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.isScrolling = YES;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //[super scrollViewDidEndDragging:scrollView willDecelerate:decelerate];    // pull to refresh

    if(!decelerate) {
        self.isScrolling = NO;
        [self callMethodThatRequiresTableViewAndArrayOfDataToBePassedIn];
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.isScrolling = NO;
    [self callMethodThatRequiresTableViewAndArrayOfDataToBePassedIn];
}

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{   
    self.isScrolling = NO;
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
        self.isScrolling = NO;
}

这个解决方案非常有效。但是,我需要在4个不同的视图控制器中使用相同的逻辑,而且我不喜欢重复代码。

我不知道如何在一个类中实现上面的代码,然后在其他4个类中使用它。

我有一些想法:

  • 我有一个基类,其他视图控制器都是从它继承的。我在想,我可以将基类子类化,然后需要此代码的4个视图控件从这个新类中被子类化。此新类提供滚动委托方法的实现。但是如何调用该方法并传入tableView和dataArray。
  • 子类UITableView并在子类中实现这4种方法。这意味着我可以通过tableView ok。但是,数据源是数组则更有问题。

有没有人对这个问题的解决有什么建议呢?非常感谢

EN

回答 1

Stack Overflow用户

发布于 2015-08-09 11:38:52

我觉得你走上正轨了

代码语言:javascript
复制
I have a base class that all other view controllers inherit from. I was thinking I could subclass the base class and then the 4 view controls that require this code are subclassed from this new class. This new class provides the implementation for the scroll delegate methods.

现在你的问题是

代码语言:javascript
复制
how do I call the method and passing in the tableView and dataArray

为此,您可以在基类中接受委托方法的帮助,创建协议。

yourBaseClass.h

代码语言:javascript
复制
@protocol  MyDelegate <NSObject>
@required
-(void)callMethodThatRequiresTableViewAndArrayOfDataToBePassedIn;
@end   

@interface yourBaseClass:UITableViewController
@property (nonatomic, weak) id<MyDelegate> myDelegate;
@end 

yourBaseClass.m

代码语言:javascript
复制
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.isScrolling = YES;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //[super scrollViewDidEndDragging:scrollView willDecelerate:decelerate];    // pull to refresh

    if(!decelerate) {
        self.isScrolling = NO;
       if(self.myDelgate && [self.myDelegate respondsToSelector(callMethodThatRequiresTableViewAndArrayOfDataToBePassedIn)])
        {
            [self.myDelegate callMethodThatRequiresTableViewAndArrayOfDataToBePassedIn];
        }

    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.isScrolling = NO;
    if(self.myDelgate && [self.myDelegate respondsToSelector(callMethodThatRequiresTableViewAndArrayOfDataToBePassedIn)])
    {
        [self.myDelegate callMethodThatRequiresTableViewAndArrayOfDataToBePassedIn];
     }

}

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
  {   
      self.isScrolling = NO;
  }

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
  {
      self.isScrolling = NO;
  }

现在,您必须在子类中实现myDelegate方法callMethodThatRequiresTableViewAndArrayOfDataToBePassedIn

是的还设置了

代码语言:javascript
复制
self.myDelegate=self;

在子类viewDidLoad方法中。

使用这种方法,您不需要传递任何tableView或dataArray。

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31903235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档