我正在尝试使用this类实现内联日期选择器视图,以下是此类示例中显示的代码:
var cells:NSMutableArray = []
let datePickerCell = DatePickerCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
//in ViewDidLoad :
cells = [datePickerCell]
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
// Get the correct height if the cell is a DatePickerCell.
let cell = self.tableView(tableView, cellForRowAtIndexPath: indexPath)
if let datePickerCell = cell as? DatePickerCell {
return datePickerCell.datePickerHeight()
}
return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Deselect automatically if the cell is a DatePickerCell.
let cell = self.tableView(tableView, cellForRowAtIndexPath: indexPath)
if let datePickerCell = cell as? DatePickerCell {
datePickerCell.selectedInTableView(tableView)
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cells.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return cells[indexPath.row] as! UITableViewCell
}
我希望在点击按钮时插入行,使用以下代码:
func insert() {
cells.addObject(datePickerCell)
let insertionIndexPath = NSIndexPath(forRow: cells.count - 1, inSection: 0)
tableView.insertRowsAtIndexPaths([insertionIndexPath], withRowAnimation: .Automatic)
}
我得到的是没有日期选择器视图的空单元格,我还尝试将另一个datePickerCell添加到单元格数组:
cells = [datePickerCell , datePickerCell]
同样的事情发生了,只有空单元
image显示空单元格,只显示一个日期选择器视图
任何帮助?
答案 0 :(得分:0)
只需用
替换按钮功能即可func insert() {
let datePickerCell = DatePickerCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
cells.addObject(datePickerCell)
self.tableView.beginUpdates()
self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: cells.count-1, inSection: 0)], withRowAnimation: .Bottom)
self.tableView.endUpdates()
}