I have a UITableView populating this sample Dictionary.
var WholeDict = [
"Animals" : ["Cat","Dog"],
"Fruits" : ["Apple","Orange"]
]
The content is too small. So I am choosing the grouped UITableView style, so that I can avoid the unwanted cells that is showing below the content in UITableView. But the problem is, when I choose grouped style, the cells under each sections are not going under header when scrolling. The scrolling moves the header first. Grouped style: cells scrolling along with header & Plain style: cells sliding under header. This is what I am doing in code
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
}
}
So how can I avoid the extra spaces below the tableview and at the same time use the sliding of rows under headers?
EDIT I am using walters answer. It is working for me. But then I had another issue. When I am scrolling beyond the last row, then the screen will show only the footer that I had added. So I added another workaround.
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)
}
}