【问题标题】:iPhone scroll UITableview by swiping out side the table viewiPhone通过滑出表格视图来滚动UITableview
【发布时间】:2013-01-22 09:06:03
【问题描述】:

就我而言,我有 UIView 和 UITableView 控制器。我想要的是通过滑动 UIView 来滚动表格视图。我为 UIView 添加了向上滑动和向下滑动手势。当我在此视图上滑动时,沿滑动方向滚动表格视图。有可能吗?

【问题讨论】:

  • 是的,一个简单的方法是使用 UITableView 作为一个普通的 UIScrollView(UITableView 是一个 UIScrollView,继承自那个类,所以你可以使用它的所有 UIScrollView 方法,以及相关的协议方法)
  • @meronix:感谢您的快速重播。冷,请发布一些示例代码?
  • @Muhammad:UIView 不是 UITableview 的子类

标签: iphone ios ipad uitableview


【解决方案1】:

从您的 viewDidLoad 调用此方法。 此方法将禁用 tableview 的滚动以防止手动滚动。假设您有 100 行高度为 30 并且 myview 是必须执行滚动的视图。 myview上添加了滑动手势,设置滑动方向很重要

-(void)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 手势时,它会将 tableview 中当前可见的最后一行滚动到顶部 当该方法接收到 swipeDown 时,它会将当前可见的第一个行滚动到底部

-(void)motion:(UISwipeGestureRecognizer *)sender {

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 方法中添加以下行

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      • 1970-01-01
      • 2021-03-24
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 2015-08-06
      相关资源
      最近更新 更多