Swift 3 UITableView 部分显示在列表后面

标签 swift uitableview swift3 uitableviewsectionheader

我正在尝试使用 swift 3 UITableViewController 创建产品列表,但这些部分显示在产品列表之后。一定有什么东西我丢失了,到处都找不到

这是一个简化的代码

let data = [["0,0", "0,1", "0,2"], ["1,0", "1,1", "1,2"], ["2,0", "2,1", "2,2"],  ["3,0", "3,1", "3,2"]]

override func numberOfSections(in tableView: UITableView) -> Int {
    return data.count//productCategoriesSections.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data[section].count//productCategories[section].count
}


 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "defaultCategoryCell", for: indexPath) as! CategoryDefaultTableViewCell
    cell.titleTextView.text = data[indexPath.section][indexPath.row]
    return cell
 }

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    return "Section \(section)"
}

screenshot

最佳答案

这是因为您使用了错误的回调。

使用 override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? 而不是 override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) ->字符串?

前者会将文本作为标题放置在该部分上方。后者将其放置在底部。

关于Swift 3 UITableView 部分显示在列表后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43822677/

相关文章:

swift - 在类中创建自定义操作以在 Interface Builder 中使用

ios - reloadRowsAtIndexPaths 不更新我的单元格数据

swift - 通过索引访问 AnyHashable

ios - 执行 UINavigationController 异常

html - 如何检查字符串是否包含html标签最通用的方法

ios - 使用 NSURLSession 发出同步请求

ios - Swift Admob 控制台显示模拟器设备 ID 但在 iPhone 上运行时不显示?

iphone - 自定义 UITableViewCell 不显示 Storyboard中的标签

iphone - UITableViewCell 中的多种颜色

ios - UITableview 分隔符不为 iOS9 隐藏