插入内联日期选择器视图单元格 - swift

insert inline date pickerView cell - swift

我正在尝试使用 this class 实现内联日期选择器视图,这里是 class 示例中显示的代码:

 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 显示空单元格和只有一个日期选择器视图

有什么帮助吗?

只需将按钮的功能替换为

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