【发布时间】: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)
}
}
【问题讨论】:
-
另一种解决方法是从表格视图中删除分隔线(
separatorStyle或separatorColor),然后为cellForRowAtIndexPath中的每个单元格手动绘制它们。这样可以避免页脚问题。
标签: ios iphone swift uitableview uitableviewsectionheader