2013-01-22 25 views
4

在我的情况下,我有UIView和UITableView控制器。我想要的是通过滑动UIView来滚动表格视图。我已经添加了向上滑动并向下滑动了UIView的手势。当我在该视图上滑动时,沿着滑动方向滚动表格视图。它是否可行?iPhone滚动UITableview通过刷出表格视图

+3

是的,一个简单的方法是使用的UITableView作为一个正常的UIScrollView(UITableView的是一个UIScrollView,从类继承,所以你可以使用所有的UIScrollView的方法它和相关的协议方法) – meronix

+0

@meronix:谢谢你的快速回放。冷,请发布一些示例代码? – Guru

+0

@Muhammad:UIView不是UITableview的子类 – Guru

回答

2

调用此方法。 此方法将禁用滚动tableview以防止手动滚动。假设您有100行高度30,myview是必须执行滚动的视图。滑动手势是在MyView的加入,它设置轻扫的方向是很重要的

- (无效)initialSetup {

[self.tableView setScrollEnabled:NO]; 
int numberOfRows=100; 
int rowHeight=30; 
myview=[[UIView alloc]initWithFrame:CGRectMake(280, 0, 30, numberOfRows*rowHeight)]; 
[myview setBackgroundColor:[UIColor yellowColor]]; 
[self.view addSubview:myview]; 
UISwipeGestureRecognizer *swipeDown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(motion:)]; 
[swipeDown setDirection:UISwipeGestureRecognizerDirectionDown]; 
[myview addGestureRecognizer:swipeDown]; 
UISwipeGestureRecognizer *swipeUp=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(motion:)]; 
[swipeUp setDirection:UISwipeGestureRecognizerDirectionUp]; 
[myview addGestureRecognizer:swipeUp];} 

当滑动手势发生任swipeUp或swipeDown的以下方法将被称为。 当该方法接收一swipeUp手势当该方法接收一swipeDown它滚动所述第一当前可见行到它滚动在的tableview顶端 最后目前可见行底部

- (无效)运动:(UISwipeGestureRecognizer *)发件人 {

if (sender.direction==UISwipeGestureRecognizerDirectionUp) { 
    [self.tableView scrollToRowAtIndexPath:[self.tableView.indexPathsForVisibleRows objectAtIndex:[self.tableView.indexPathsForVisibleRows count]-1] atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
}else if(sender.direction==UISwipeGestureRecognizerDirectionDown) 
{ 
    [self.tableView scrollToRowAtIndexPath:[self.tableView.indexPathsForVisibleRows objectAtIndex:0 ] atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 
} 

}

2

//让MyView的是从中要滚动你的tableview(myTableview)认为

//现在从viewDidLoad中添加以下行您的viewDidLoad方法

UISwipeGestureRecognizer * swipegesture = [[UISwipeGestureRecognizer alloc]init]; 
swipegesture =[myTableview.gestureRecognizers objectAtIndex:1]; 
[myView addGestureRecognizer:swipegesture];