2012-03-21 61 views
0

我想在单个屏幕上制作两个表格,这样如果表格A向下滚动,则表格B同时向上滚动。有人可以编码或为我提供任何简单的方法来做到这一点。单个屏幕上的两个UITableView

+0

你能解释你到底想做什么,所以我们可以给你适当的解决方案。 – Rupesh 2012-03-21 12:56:55

回答

2

正如吴宝提到的那样,您必须使用UIScrollViewDelegate。但是你必须检查一下,哪个scrollView在拖动/活动。因为否则你会遇到这样的问题,即你将从这两个滚动视图中获得委托回调,并且它们将同时给予彼此的更改,导致无限循环/无限滚动。

详细,你必须检查:- (void)scrollViewDidScroll:(UIScrollView *)scrollView

但是你要记住以前的偏移,这样你就知道值的变化。 (或者你的观点具有相同的高度。然后你可以只使用contentSize.height-offset作为其他视图偏移

我会尽量把它写下来一点点(未经测试):

@interface ViewController() <UITableViewDelegate,UIScrollViewDelegate> 

// instances of your tableviews 
@property (nonatomic, strong) UITableView *tableLeft; 
@property (nonatomic, strong) UITableView *tableRight; 

// track active table 
@property (nonatomic, strong) UIScrollView* activeScrollView; 

// helpers for contentoffset tracking 
@property (nonatomic, assign) CGFloat lastOffsetLeft; 
@property (nonatomic, assign) CGFloat lastOffsetRight; 

@end 


@implementation ViewController 

- (void) viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.tableLeft.delegate = self; 
    self.tableRight.delegate = self; 
} 

– (void) scrollViewWillBeginDragging: (UIScrollView*) scrollView 
{ 
    self.activeScrollView = scrollView; 
    self.tableViewRight.userInterActionEnabled = (self.tableViewRight == scrollView); 
    self.tableViewLeft.userInterActionEnabled = (self.tableViewLeft == scrollView); 
} 

- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 
{ 
    if(!decelerate) { 
    self.activeScrollView = nil; 
    self.tableViewRight.userInterActionEnabled = YES; 
    self.tableViewLeft.userInterActionEnabled = YES; 
    } 
} 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
    self.activeScrollView = nil; 
    self.tableViewRight.userInterActionEnabled = YES; 
    self.tableViewLeft.userInterActionEnabled = YES; 
} 

- (void) scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    if(self.activeScrollView == self.tableViewLeft) 
    { 
    CGFloat changeLeft = self.tableViewLeft.contentOffset.y - self.lastOffsetLeft; 
    self.tableViewRight.contentOffset.y += changeLeft; 
    } 
    else if (self.activeScrollView == self.tableViewRight) 
    { 
    CGFloat changeRight = self.tableViewRight.contentOffset.y - self.lastOffsetRight; 
    self.tableViewLeft.contentOffset.y += changeRight; 
    } 

    self.lastOffsetLeft = self.tableViewLeft.contentOffset.y; 
    self.lastOffsetRight = self.tableViewRight.contentOffset.y; 
} 

@end 

这就是它基本上它也锁定不活动的滚动视图因为滚动都会导致丑陋的行为contentOffset.y += changeLeft;可能不会工作你必须创建一个新的CGPoint/CGSize

+0

你能否提供一些代码..很难gr ab .. – turtle 2012-03-21 13:23:11

+0

编辑:添加代码示例(未测试) – calimarkus 2012-03-21 14:16:46

+0

感谢jaydee ...我试图通过我自己来实现它..如果我能够成功运行它,将通知您。 – turtle 2012-03-21 16:10:34

3

UITableView是从UIScrollView派生的,所以可以使用一个表视图的viewDidScroll委托方法,来控制其他表视图的滚动位置。