Skip to content

onlyAngelia/YAAdapterTableViewWithResponderChain

Repository files navigation

ARTableView.podspec

CI Status Version License Platform

Example

1.create an adapter extends UITableViewAdapter,overwrite the delegate that you will use

2.Then setting UITableView

 [_tableView setAdapter:[self adapter]]

3.Next overwriting the func in tableView's superclass

- (void)rountEvent:(NSString *)eventName userInfo:(NSDictionary *)userInfo

4.Last create NSInvocation according to eventName

- (void)rountEvent:(NSString *)eventName userInfo:(NSDictionary *)userInfo
{
    NSInvocation *invocation = [self strategyDictionary][eventName];
    [invocation setArgument:&userInfo atIndex:2];
    [invocation invoke];
}
- (NSDictionary *)strategyDictionary{
    
    NSDictionary *strategyDictionary = @{kCCellSeletedEventName:[self createInvocationWithSeletor:@selector(jumpToController:)]};
    return strategyDictionary;
    
}

1.创建一个adapter 继承自UITableViewAdapter,在新创建的adapter中重写可能会用到的类 2.设置tableView和adapter关联一起

 [_tableView setAdapter:[self adapter]]

3.接收tableView的点击事件以及滑动事件,因这里是基于ResponderChain传递,所以只要在tableview的super中实现

- (void)rountEvent:(NSString *)eventName userInfo:(NSDictionary *)userInfo

该方法即可监听tableView的事件。 好处:避免了大量的blockdelegate

4.采用strategy模式避免n多if-else

每个点击事件最终要执行不同的@selector,所以我们可以采用策略模式,直接取消[self performSelector:<#(nonnull SEL)#> withObject:<#(nullable id)#>]@selector定义为更深一层NSInvocation,用NSDictionary存储

- (void)rountEvent:(NSString *)eventName userInfo:(NSDictionary *)userInfo
{
    NSInvocation *invocation = [self strategyDictionary][eventName];
    [invocation setArgument:&userInfo atIndex:2];
    [invocation invoke];
}
- (NSDictionary *)strategyDictionary{
    
    NSDictionary *strategyDictionary = @{kCCellSeletedEventName:[self createInvocationWithSeletor:@selector(jumpToController:)]};
    return strategyDictionary;
    
}

Requirements

you need to import"UITableView+adapter"

Installation

ARTableView.podspec is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'ARTableView', '~> 1.0.2'

Then run a pod install inside your terminal

Author

onlyAngelia, 关门滢

License

ARTableView.podspec is available under the MIT license. See the LICENSE file for more info.