xcode UITableView 分页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11900555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:12:07  来源:igfitidea点击:

UITableView Pagination

iphoneiosxcodeuitableview

提问by Mohanadevi

My app has custom UITableViewcells. I want to show only one cell at a time - next cell should show partially. In ScrollView you can set isPagingEnabledto YES.

我的应用程序有自定义UITableView单元格。我想一次只显示一个单元格 - 下一个单元格应该部分显示。在 ScrollView 中,您可以设置isPagingEnabled为 YES。

But how can i do above in UITableView?

但是我怎么能在上面做UITableView呢?

Thanks

谢谢

回答by J?rn Eyrich

Note that UITableViewinherits from UIScrollView, so you can set pagingEnabledto YESon the table view itself.

请注意,UITableView继承自UIScrollView,因此您可以在表视图本身上设置pagingEnabledYES

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

当然,这只有在所有单元格和表格视图本身的高度相同时才有效。

If you want to always have a cell start at the top of the table view after scrolling, you could use a UIScrollViewDelegateand implement something like this.

如果您想在滚动后始终在表格视图的顶部开始一个单元格,您可以使用 aUIScrollViewDelegate并实现类似的功能。

- (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 lets you adjust at which content offset a scroll action will end.

这使您可以调整滚动操作将结束的内容偏移量。

回答by Josh Wolff

Swift 5 basic version, but it does not work that well. I needed to customize it for my own use to make it work.

Swift 5 基本版,但效果不佳。我需要自定义它以供我自己使用以使其工作。

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

Customized 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)
         }
    }
}

回答by Dan Ray

I don't think I'd use a UITableView for this at all.

我认为我根本不会为此使用 UITableView。

I think I'd use a UIScrollView with a tall stack of paged content. You could dynamically rebuild that content on scrolling activity, so you mimic the memory management of UITableView. UIScrollView will happily do vertical paging, depending on the shape of its contentView's frame.

我想我会使用 UIScrollView 和一大堆分页内容。您可以在滚动活动中动态重建该内容,因此您可以模仿 UITableView 的内存管理。UIScrollView 会很高兴地进行垂直分页,具体取决于其 contentView 框架的形状。

In other words, I suspect it's easier to make a UIScrollView act like a table than to make a UITableView paginate like scroll view.

换句话说,我怀疑让 UIScrollView 像表格一样比让 UITableView 像滚动视图一样分页更容易。