简体   繁体   中英

Horizontal scrolling of tableview with swipe gesture left and right

I am trying to implement the uitableview with horizontal scroll. I was taken scrollview and added tableview on scrollview. 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.

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.

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 .

Try to add that UITableView delegate method :

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

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

write only the following codes in 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:

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

- (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. this will fix all the thing you doesn't need to take scrollview also.

if the requirement is other case ... Please explain your requirement bit meow detailed. So we can have a solution.

Just use UICollectionView !

Change the scrollDirection property of your flow layout to UICollectionViewScrollDirectionHorizontal .

[yourFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM