简体   繁体   English

左右滑动手势可水平滚动tableview

[英]Horizontal scrolling of tableview with swipe gesture left and right

I am trying to implement the uitableview with horizontal scroll. 我正在尝试通过水平滚动实现uitableview I was taken scrollview and added tableview on scrollview. 我被带上scrollview并在scrollview上添加了tableview。 I set the content size of the scrollview is greater than frame size. 我将滚动视图的内容大小设置为大于框架大小。 So that it can scroll horizontally. 使它可以水平滚动。 But when I added swipe gesture on uitableview it is not performing the actions. 但是,当我在uitableview上添加滑动手势时,它不执行操作。

code snippet is here 代码段在这里

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];


        }
}

I want to reload table when it swiped left or right. 我想在向左或向右滑动时重新加载表格。 But for now it is not working. 但是目前它不起作用。

If you add tableView in a scrollView , your gestureRecognizer maybe covered by scrollView's gestureRecognizer. 如果在scrollView中添加tableView,则scrollRecognizer可能覆盖了您的gestureRecognizer。

you can try implement you code in 你可以尝试实现你的代码

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;
}

and track scroll direction yourself. 并自己跟踪滚动方向。

ps:remember to adjust scrollView's contentSize as tableView.frame.size.width*numberOfYourTableView . ps:请记住将scrollView的contentSize调整为tableView.frame.size.width*numberOfYourTableView

Try to add that UITableView delegate method : 尝试添加该UITableView委托方法:

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

By the way, you can instantiate only one UIGestureRecognizer, by setting the direction property like that : 顺便说一句,通过设置direction属性,您只能实例化一个UIGestureRecognizer:

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

when I need to display a horizontal table view, I just use: 当我需要显示水平表格视图时,只需使用:

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

If I need to track scrolling I use: 如果需要跟踪滚动,请使用:

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

Regards, Venelin 问候,Venelin

write only the following codes in SwipeRecognizer. 在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];


    }
}

and write tableview delegate method (for Data) as the following: 并按以下方式编写tableview委托方法(用于Data):

- (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
}

and yes don't forget to add the following to viewDidLoad method 是的,别忘了将以下内容添加到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];
}

if your requirement is showing content in tableview (only 1 row and when you scroll horizontally) the best way will be transforming the tableview. 如果您的要求是在表格视图中显示内容(仅1行,并且在水平滚动时),则最好的方法是转换表格视图。 this will fix all the thing you doesn't need to take scrollview also. 这将解决所有您不需要使用scrollview的问题。

if the requirement is other case ... Please explain your requirement bit meow detailed. 如果要求是其他情况...请详细说明您的要求。 So we can have a solution. 因此,我们可以找到解决方案。

Just use UICollectionView ! 只需使用UICollectionView

Change the scrollDirection property of your flow layout to UICollectionViewScrollDirectionHorizontal . 将流布局的scrollDirection属性更改为UICollectionViewScrollDirectionHorizontal

[yourFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 hidesBarsOnSwipe禁用左右滑动手势 - hidesBarsOnSwipe disables Left & Right Swipe Gesture 如何在iOS SDK中的TableView中向左和向右滑动 - how to left and right swipe in tableview in iOS sdk 在表格视图中滑动手势 - Swipe gesture in tableview 如何在详细视图UIimage中为右/左创建滑动手势 - How to create swipe gesture for right/left in detailed view UIimage iOS从左向右滑动到上一页,并带有手势效果示例 - iOS swipe left to right to previous page with gesture effect example iPad水平滑动原因页面向右或向左跳转并返回 - iPad Horizontal Swipe Cause Page to Jump Right or Left and Return 我可以仅对UITableViewCellEditingStyleDelete使用向左滑动,并使向右滑动手势响应另一种方法吗? - Can I use only left swipe for UITableViewCellEditingStyleDelete and make right swipe gesture respons to another method? 在垂直滚动时未调用collectionView上的向左/向右滑动 - Swipe Left/Right on collectionView not called while scrolling vertically 在Swift 3中滑动时如何向TableView添加向左滑动手势并返回单元格位置 - How to add a Swipe Left gesture to a TableView and return the cell location when swiped in Swift 3 当滚动方向为水平时,从左到右填充UICollectionView - Populate UICollectionView from left to right when scrolling direction is horizontal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM