UITableView Pagination

My application has custom UITableView cells. I want to show only one cell at a time - the next cell should be partially displayed. In ScrollView, you can set isPagingEnabled to YES.

But how can I do the above in a UITableView ?

thanks

+4
source share
3 answers

Note that UITableView inherits from UIScrollView , so you can set pagingEnabled to YES in the table view itself.

Of course, this will only work if all the cells and the table view have the same height.

If you want to always run the cell at the top of the table view after scrolling, you can use UIScrollViewDelegate and implement something like this.

 - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { UITableView *tv = (UITableView*)scrollView; NSIndexPath *indexPathOfTopRowAfterScrolling = [tv indexPathForRowAtPoint: *targetContentOffset ]; CGRect rectForTopRowAfterScrolling = [tv rectForRowAtIndexPath: indexPathOfTopRowAfterScrolling ]; targetContentOffset->y=rectForTopRowAfterScrolling.origin.y; } 

This allows you to adjust what content the scroll action shifts to.

+6
source

Swift 5 is the basic version, but it does not work so well. I needed to configure it for myself so that it worked.

 func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { if let tv = scrollView as? UITableView { let path = tv.indexPathForRow(at: targetContentOffset.pointee) if path != nil { self.scrollToRow(at: path!, at: .top, animated: true) } } } 

Individual version

 // If velocity is less than 0, then scrolling up // If velocity is greater than 0, then scrolling down if let tv = scrollView as? UITableView { let path = tv.indexPathForRow(at: targetContentOffset.pointee) if path != nil { // >= makes scrolling down easier but can have some weird behavior when scrolling up if velocity.y >= 0.0 { // Assumes 1 section // Jump to bottom one because user is scrolling down, and targetContentOffset is the very top of the screen let indexPath = IndexPath(row: path!.row + 1, section: path!.section) if indexPath.row < self.numberOfRows(inSection: path!.section) { self.scrollToRow(at: indexPath, at: .top, animated: true) } } else { self.scrollToRow(at: path!, at: .top, animated: true) } } } 
0
source

I do not think that I would use a UITableView for this at all.

I think I would use a UIScrollView with a high stack of paged content. You can dynamically rebuild this content while scrolling, so you are simulating UITableView memory management. UIScrollView will happily perform vertical paging, depending on the shape of its contentView frame.

In other words, I suspect that making a UIScrollView operation easier than a UITableView table, such as a scroll view.

-1
source

All Articles