【问题标题】:UITableView rows sliding under UITableViewHeaderSectionUITableView 行在 UITableViewHeaderSection 下滑动
【发布时间】:2016-08-30 19:18:48
【问题描述】:

我有一个 UITableView 填充这个示例字典。

var WholeDict = [
    "Animals" : ["Cat","Dog"],
    "Fruits" : ["Apple","Orange"]
]

内容太少。所以我选择了分组的 UITableView 样式,这样我就可以避免在 UITableView 的内容下方显示不需要的单元格。但问题是,当我选择分组样式时,每个部分下的单元格在滚动时不会在标题下。滚动首先移动标题。 Grouped style: cells scrolling along with header & Plain style: cells sliding under header。这就是我在代码中所做的事情

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let allkeys = Array(WholeDict.keys)
    return allkeys[section]
}

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {
        headerView.textLabel?.textAlignment = .Center
    }
}

那么如何避免tableview下方多余的空格,同时使用headers下的行滑动呢?

编辑 我正在使用沃尔特斯的答案。它对我有用。但后来我遇到了另一个问题。当我滚动到最后一行之外时,屏幕将只显示我添加的页脚。所以我添加了另一个解决方法。

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    let visiblerows = MyTable.indexPathsForVisibleRows
    if visiblerows?.count == 0 {
        let ScrollToIndexPath = NSIndexPath(forRow: 0, inSection: WholeDict.count - 1)
        MyTable.scrollToRowAtIndexPath(ScrollToIndexPath, atScrollPosition: UITableViewScrollPosition.Top, animated: true)
    }
}

【问题讨论】:

  • 另一种解决方法是从表格视图中删除分隔线(separatorStyleseparatorColor),然后为cellForRowAtIndexPath 中的每个单元格手动绘制它们。这样可以避免页脚问题。

标签: ios iphone swift uitableview uitableviewsectionheader


【解决方案1】:

UITableView 将始终添加空白“单元格”以填充空间。为了使它们消失,您应该创建一个填充最后填充行底部和 tableView 底部之间的空间的 footerView。这是一个简单的示例,但您可能需要根据添加单元格的方式来移动它。 tableView 的contentSize 属性的高度将等于 tableView 的填充部分。

 let contentSize = self.tableView.contentSize
 let footer = UIView(frame: CGRect(x: self.tableView.frame.origin.x, 
                                   y: self.tableView.frame.origin.y + contentSize.height, 
                               width: self.tableView.frame.size.width,
                              height: self.tableView.frame.height - self.tableView.contentSize.height))

  self.tableView.tableFooterView = footer

【讨论】:

  • 谢谢。这是工作。我已经用我的解决方法编辑了我的问题。请也检查一下。
  • 如果您使用故事板,您还可以在表格视图下方拖动 uiview 以添加页脚视图
  • 页脚视图不需要是特定的高度(也可以是 0)。只需简单地拥有它 - 空白单元格就会消失。
【解决方案2】:

如果您希望固定节标题,则可以使用此属性。

self.tableView.style = UITableViewStyleGrouped

它使您的部分标题粘在行上并随着它们滚动。

【讨论】:

  • 我不希望我的部分标题被修复。但是使用分组样式会删除不需要的行。我希望它与普通样式的“在节标题下滑动的行”一起使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多