【问题标题】:Horizontal scrolling of tableview with swipe gesture left and right左右滑动手势的tableview水平滚动
【发布时间】:2013-12-13 10:47:26
【问题描述】:

我正在尝试使用水平滚动来实现uitableview。我被采取了滚动视图并在滚动视图上添加了表格视图。我设置滚动视图的内容大小大于帧大小。这样它就可以水平滚动。但是当我在 uitableview 上添加滑动手势时,它并没有执行操作。

代码 sn-p 在这里

leftswipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(SwipeRecognizer:)];
    leftswipe.delegate = self;
    leftswipe.direction = UISwipeGestureRecognizerDirectionLeft;

    rightswipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(SwipeRecognizer:)];
    rightswipe.delegate = self;
    rightswipe.direction = UISwipeGestureRecognizerDirectionRight;

- (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender
{
        if ( sender.direction == UISwipeGestureRecognizerDirectionLeft )
        {
            NSLog(@" *** SWIPE LEFT ***");
            index1++;
            self.tableHeaderLabel.text = [self.categoriesLabelArray objectAtIndex:index1];
            [self.feedsTableView reloadData];

        }
        if ( sender.direction == UISwipeGestureRecognizerDirectionRight ){
            NSLog(@" *** SWIPE RIGHT ***");
            index1--;
            self.tableHeaderLabel.text = [self.categoriesLabelArray objectAtIndex:index1];
            [self.feedsTableView reloadData];


        }
}

我想在向左或向右滑动时重新加载表格。 但目前它不起作用。

【问题讨论】:

  • 您想要水平滚动视图和水平滑动手势管理?有冲突是正常的……你应该考虑用两根手指做手势
  • 水平滚动视图实现成功但滑动手势未实现
  • 您是否在任何视图中添加了左滑和右滑?
  • 这和xcode有什么关系?
  • 您不需要滑动手势。当 tableViews 框架足够大并且您将 scrollViews contentSize 设置为等于 tableViews 边界时,scrollView 将默认为您提供滚动功能。如果你想要滚动事件管理,使用 scrollViewDelegate 协议

标签: ios objective-c


【解决方案1】:

如果你在 scrollView 中添加 tableView ,你的gestureRecognizer 可能会被scrollView 的gestureRecognizer 覆盖。

你可以尝试在

中实现你的代码
CGFloat lastOffset_x = 0;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
     CGFloat currentOffSet_x = scrollView.contentOffSet.x;
     if(lastOffset_x > currentOffSet_x){
         //direction left
     }else{
         //direction right
     }
     lastOffset_x = scrollView.contentOffSet.x;
}

并自己跟踪滚动方向。

ps:记得调整scrollView的contentSize为tableView.frame.size.width*numberOfYourTableView

【讨论】:

  • 你好,我编辑了我的答案,请试一试,让我知道它是否有效。:)
  • 我试过你的代码。但是在 tableview 反弹到顶部 currentOffSet_x 后变为零。应用程序崩溃。那你能编辑一下代码吗?
【解决方案2】:

尝试添加那个 UITableView 委托方法:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}

顺便说一句,你可以通过像这样设置direction属性来实例化一个UIGestureRecognizer:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIGestureRecognizer *recognizer;
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognizer:)];
    recognizer.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
    [tableView addGestureRecognizer:recognizer];
}

【讨论】:

    【解决方案3】:

    在 SwipeRecognizer 中只写以下代码。

    - (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender
    {
        if ( sender.direction == UISwipeGestureRecognizerDirectionLeft )
        {
            NSLog(@" *** SWIPE LEFT ***");
            index1++;
    
            [self.feedsTableView reloadData];
    
        }
        if ( sender.direction == UISwipeGestureRecognizerDirectionRight ){
            NSLog(@" *** SWIPE RIGHT ***");
            index1--;
    
            [self.feedsTableView reloadData];
    
    
        }
    }
    

    并编写tableview委托方法(用于数据)如下:

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        return [self.categoriesLabelArray objectAtIndex:index1];
    
    
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if(index1 == 0)
            //set tableCellDataArray
        if(index1 == 1)
            //set tableCellDataArray
        if(index1 == 2)
            //set tableCellDataArray
        //...
        //...
        if(index1 == n)
            //set tableCellDataArray
    }
    

    是的,不要忘记在 viewDidLoad 方法中添加以下内容

    - (void)viewDidLoad
    {
         leftswipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(SwipeRecognizer:)];
         leftswipe.delegate = self;
         leftswipe.direction = UISwipeGestureRecognizerDirectionLeft;
    
         rightswipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(SwipeRecognizer:)];
         rightswipe.delegate = self;
         rightswipe.direction = UISwipeGestureRecognizerDirectionRight;
    
         [self.feedsTableView addGestureRecognizer: leftswipe];
         [self.feedsTableView addGestureRecognizer: rightswipe];
    }
    

    【讨论】:

      【解决方案4】:

      如果您的要求是在 tableview 中显示内容(只有 1 行并且当您水平滚动时)最好的方法是转换 tableview。这将解决您不需要滚动视图的所有问题。

      如果要求是其他情况...请详细说明您的要求。所以我们可以有一个解决方案。

      【讨论】:

        【解决方案5】:

        只需使用UICollectionView

        将流布局的scrollDirection 属性更改为UICollectionViewScrollDirectionHorizontal

        [yourFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        

        【讨论】:

          【解决方案6】:

          当我需要显示水平表格视图时,我只使用:

          -(void)setupProductsTableView {
                  self.innerProductsTable.transform = CGAffineTransformMakeRotation(-M_PI_2);
                  self.innerProductsTable.delegate = self;
                  self.innerProductsTable.dataSource = self;
              }
          

          如果我需要跟踪滚动,我会使用:

          -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
                  int lastVisibleCell = [[[tableView indexPathsForVisibleRows] lastObject ] row];
                  //do some with last displayed cell
              }
          

          问候, 维尼林

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-07-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-01-14
            • 2011-07-23
            • 1970-01-01
            相关资源
            最近更新 更多