需求:当外层tableview滑动到一定位置的时候保持不动,然后里层的tableview滑动.当里层tableview在最顶端的时候保持不动,滑动外层tableview.
思路:让两个tableview的手势都一起触发.当滑动到零界点其中一个tableview的contentOffset不变,另一个tableview继续滑动.
定义FirstTableView继承UITableView
@interface FirstTableView : UITableView
@end
#import "FirstTableView.h"
@implementation FirstTableView
// 是否支持多手势触发,返回YES,则可以多个手势一起触发方法(子tableview也可以滑动),返回NO则为互斥
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
@end
定义SecondTableView继承UITableView
#import <UIKit/UIKit.h>
@interface SecondTableView : UITableView<UITableViewDelegate,UITableViewDataSource>
{
UITableViewCell *cells[10];
BOOL isCanScroll;
}
@end
#import "SecondTableView.h"
@implementation SecondTableView
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
if(self = [super initWithFrame:frame style:style])
{
self.dataSource = self;
self.delegate = self;
self.tableFooterView = [UIView new];
isCanScroll = NO;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(test:)
name:@"test"
object:nil];
for(int i = 0 ; i < 10; i++)
{
cells[i] = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
cells[i].frame = CGRectMake(0, 0, self.frame.size.width, 44.0f);
}
[self reloadData];
}
return self;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return cells[indexPath.row].frame.size.height;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cells[indexPath.row].textLabel.text = @"2";
return cells[indexPath.row];
}
- (void)test:(NSNotification *)n
{
isCanScroll = YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if(isCanScroll == NO)
{
[scrollView setContentOffset:CGPointZero animated:NO];
}
else
{
if(scrollView.contentOffset.y <= 0)
{
isCanScroll = NO;
[[NSNotificationCenter defaultCenter] postNotificationName:@"test2" object:nil];
}
}
}
@end
viewcontroller.直接将FristTablview的contentOffset判断逻辑写在这里,没有封装.
#import <UIKit/UIKit.h>
#import "FirstTableView.h"
#import "SecondTableView.h"
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
FirstTableView *aTableView;
SecondTableView *bTableView;
UITableViewCell *cells[10];
}
@end
#import "ViewController.h"
@interface ViewController ()
{
BOOL isCanScrollView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
aTableView = [[FirstTableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
aTableView.delegate = self;
aTableView.dataSource = self;
aTableView.tableFooterView = [UIView new];
[self.view addSubview:aTableView];
isCanScrollView = YES;
for(int i = 0 ; i < 10; i++)
{
cells[i] = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
cells[i].frame = CGRectMake(0, 0, self.view.frame.size.width, 44.0f);
}
bTableView = [[SecondTableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
cells[9].frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[cells[9] addSubview:bTableView];
[aTableView reloadData];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(test2:)
name:@"test2"
object:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)test2:(NSNotification *)n
{
isCanScrollView = YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if(isCanScrollView)
{
//大于这个临界值就保持不变
if(scrollView.contentOffset.y > 300)
{
[scrollView setContentOffset:CGPointMake(0, 300) animated:NO];
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil];
isCanScrollView = NO;
}
}
else
{
[scrollView setContentOffset:CGPointMake(0, 300) animated:NO];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return cells[indexPath.row].frame.size.height;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cells[indexPath.row].textLabel.text = @"1";
return cells[indexPath.row];
}
@end
源代码:https://github.com/fero2004/UITableViewInUITableView/tree/master/tableviewtest
网友评论