【问题标题】:Height broken for UIDatePicker in UITableViewCell in static cell静态单元格中 UITableViewCell 中的 UIDatePicker 的高度损坏
【发布时间】:2015-01-28 15:58:00
【问题描述】:

我正在尝试将 UIDatePicker 放入通过单击其上方的单元格来打开/关闭的 UITableViewCell 中。选择器似乎呈现所有错误。首先,这是 UITableView 类的相关代码:

// Determines if the date picker should be shown
var editingDate: Bool = false

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.section == 1 && indexPath.row == 1 { // Picker cell
        if !self.editingDate {
            return 0
        }
    }

    return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.section == 1 && indexPath.row == 0 { // Date cell
        self.editingDate = !self.editingDate
        tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 1, inSection: 1)], withRowAnimation: .Fade)
    }
}

情节提要中的表格:

我已在情节提要中将表格单元格设置为自定义高度 219。下面是我当前单击日期字段时发生的情况的动画 GIF。抱歉质量太差了。

我注意到我们可以从上面的动画 gif 中看到单元格背景变得透明(不是白色),这意味着单元格的内容视图没有拉伸到完整高度。我想知道为什么会这样?

编辑:这是用于 DatePicker 的约束:

【问题讨论】:

    标签: ios uitableview swift uidatepicker


    【解决方案1】:

    如果您使用静态单元格,您可以在情节提要编辑器中指定持有选择器的单元格的高度,您的代码可能如下所示:

    if (indexPath.section == 1 && indexPath.row == 1 && !editingDate) { // Picker cell
            return 0
    } 
    
    return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
    

    更新

    替换:

    tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 1, inSection: 1)], withRowAnimation: .Fade)
    

    与:

    tableView.beginUpdates()
    tableView.endUpdates()
    

    【讨论】:

    • 感谢您的建议,但这似乎不起作用。我刚刚注意到,我们可以从上面的动画 gif 中看到单元格背景变得透明(不是白色),这意味着单元格的内容视图没有拉伸到完整高度。我想知道为什么会这样?
    • 如果您使用静态单元格,您可以在情节提要编辑器中指定持有选择器的单元格的高度,然后您可以稍微更改您的代码。我会稍微更新一下我的答案。
    • 感谢您的建议。我在上面的问题中更新了代码,因为我正在使用静态单元格并在情节提要中设置了自定义高度。我还添加了约束的新图片。不幸的是,问题仍然存在..
    • 尝试使用:tableView.beginUpdates() 和 tableView.endUpdates() 代替:tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 1, inSection: 1)], withRowAnimation: .Fade)跨度>
    • 成功了!你想用代码更新你的答案以便我接受吗?
    【解决方案2】:

    你在使用自动布局吗?

    如果是这样,请尝试将 DatePicker 固定在 IB 中的单元格边缘,方法是按住 CTRL 键将其拖动到单元格。在所有 4 个边都固定后,再试一次。

    【讨论】:

    • 是的,我试过了,它确实使 DatePicker 更加居中,但并没有解决问题。我无法按住 CTRL 键拖动到单元格,而是拖动到单元格内的内容视图。此外,该应用程序现在会引发以下警告:Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want.
    • 是的,你是对的。单元格的 contentView 是您可以将约束拖动到的视图。但是您所说的错误意味着您的约束条件不好。可以发一张截图吗?
    • 我已经更新了问题以包括约束和完整的警告。我尝试删除它抱怨的约束(顶部)并且警告消失但仍然不起作用。
    猜你喜欢
    • 2014-09-04
    • 1970-01-01
    • 2016-08-16
    • 2011-10-20
    • 1970-01-01
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多