我最近实现了一个解决方案,允许我知道滚动视图何时完成滚动。这样,当我滚动我的表视图时,我只会在tableview完全停止移动时调用一个特定的方法。
我遵循了这里提供的答案:https://stackoverflow.com/a/8010066/2126233,但am包括下面的代码以便于阅读:
- (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个类中使用它。
我有一些想法:
有没有人对这个问题的解决有什么建议呢?非常感谢
发布于 2015-08-09 11:38:52
我觉得你走上正轨了
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.现在你的问题是
how do I call the method and passing in the tableView and dataArray为此,您可以在基类中接受委托方法的帮助,创建协议。
在yourBaseClass.h中
@protocol MyDelegate <NSObject>
@required
-(void)callMethodThatRequiresTableViewAndArrayOfDataToBePassedIn;
@end
@interface yourBaseClass:UITableViewController
@property (nonatomic, weak) id<MyDelegate> myDelegate;
@end 在yourBaseClass.m中
- (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。
是的还设置了
self.myDelegate=self;在子类viewDidLoad方法中。
使用这种方法,您不需要传递任何tableView或dataArray。
希望这能有所帮助。
https://stackoverflow.com/questions/31903235
复制相似问题