Search code examples
iosobjective-cuitableviewswipe

iOS Jump to next cell in UITableView when cell scrolled


I would like to implement a UITableView with cells that are full screen. When the user moves the cell up a portion, I would like to jump to the next cell as opposed to allowing it to scroll naturally.

I can't see a great deal of questions or answers out there on this, and perhaps it's because it's a daft idea, but the client wants to see it and the designer has designed it as such so I really must try.

Does anyone have ANY suggestions on how to implement this?

Thanks. (and thanks for pointing out my ignorance in not saying thanks!)


Solution

  • If your cells are going to be the same size as your tableView's height, set the pagingEnabled property to YES. This will make it snap to a full cell on each vertical swipe. I would ensure that the height is exactly full-screen if you do this, otherwise you'll end up with some weird offsets as you scroll.

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // or somewhere similar
        self.tableView.pagingEnabled = YES;
    }
    
    ...
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return self.tableView.frame.size.height;
    }
    

    It actually isn't a terrible idea- you could achieve similar functionality with a paging scroll-view, but this has the advantage of not needing to have all of your data loaded at once.


    Edit: I also had to make sure to disable automaticallyAdjustsScrollViewInsets on the controller, otherwise I had an undesired initial offset for the first cell, just in case you run in to that as well.