【问题标题】:UITableView PaginationUITableView 分页
【发布时间】:2012-08-10 11:17:51
【问题描述】:

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

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

谢谢

【问题讨论】:

  • 我使用了uitableview setcontentoffset。这会产生问题。请提供任何想法。
  • 如果单元格数量较少,请考虑使用基本滚动视图。

标签: iphone ios xcode uitableview


【解决方案1】:

注意UITableView 继承自UIScrollView,因此您可以在表格视图本身上将pagingEnabled 设置为YES

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

如果您希望滚动后始终在表格视图的顶部有一个单元格,您可以使用UIScrollViewDelegate 并实现类似的东西。

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

这使您可以调整滚动操作将在哪个内容偏移处结束。

【讨论】:

    【解决方案2】:

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

    定制版

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

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

      • 如果您必须在 [scrollview/tableview] 中有大量数据并且您快速滚动浏览它们,则不正确 - 那么UITableView 的内部内存管理是比本地分页更好的选择UIScrollView,至少恕我直言。
      猜你喜欢
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 2018-11-29
      • 2017-01-24
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多