Skip to main content
 首页 » 操作系统

ios之静态单元格中 UITableViewCell 中 UIDatePicker 的高度中断

2024年01月01日41unruledboy

我正在尝试将 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) 
    } 
} 

Storyboard中的表格:

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

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

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

请您参考如下方法:

如果您使用静态单元格,您可以在 Storyboard编辑器中指定包含选取器的单元格的高度,您的代码可能如下所示:

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()