ios - 在表格 View 中看不到部分

标签 ios swift uitableview

我正在尝试制作一个包含多个部分的 TableView ,但由于某些(可能是愚蠢的)原因,只有第一部分在我的 TableView 中可见。

var labels = ["label1", "label2"]

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 2
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    var sectionHeader = ""
    if section == 0 {
        sectionHeader = "first section header"
    } else if section == 1 {
        sectionHeader = "second section header"
    }
    return sectionHeader
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

    if indexPath.section == 0 {
        cell.textLabel!.text = labels[0]
    } else if indexPath.section == 1 {
        cell.textLabel!.text = labels[1]
    }

    return cell
}

第一部分的标题与带有标签文本“label1”的单元格一起出现,但找不到第二部分。

是否有某种我没有设置的设置?我在这里做错了什么。

最佳答案

你的代码看起来不错,你可以检查的东西很少

1> Tableview 的框架不够大,无法容纳 2 个部分,而且它也不能滚动。

2> heightForRow 委托(delegate)方法或 viewForHeader 或 heightforHeader 返回 0 或 nil

3> 你的约束在运行时被打破,这就是为什么 tableview 不能正确显示的原因(你需要在运行时检查控制台)

您可以在运行时启动 XCode 的可视化调试器,看看您的 TableView 是否未被其他组件等隐藏...(调试 View 层次结构)

关于ios - 在表格 View 中看不到部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32175757/

相关文章:

iphone - 如何调整 cell.textLabel 的大小?

ios - 根据JSON删除核心数据中的对象

iphone - objective-c 中的多维数组

ios - 在 Spritekit 中使用图像作为图 block 集

ios - 如果我想在 cellForRowAtIndexPath 中创建三种相似类型的单元格之一,我该如何设置 "common"属性?

swift - 在 Swift 中创建带参数的进程?

ios - FMDB 数据库查询 IOS FIND_IN_SET()

ios - 如何使用比特率在 ios 中压缩视频?

json - 如何在 SwiftUI 中显示来自 API 的 JSON 数据?

ios - 解析 JSON objectAtIndexPath