вставить встроенную ячейку выбора даты — swift

#ios #objective-c #swift #uitableview #uipickerview

#iOS #objective-c #swift #uitableview #uipickerview

Вопрос:

Я пытаюсь реализовать встроенное представление выбора даты, используя этот класс, вот код, как показано в примере для этого класса :

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

я получаю пустые ячейки без представления выбора даты, я также попытался добавить еще одну ячейку выбора даты в массив ячеек :

 cells = [datePickerCell , datePickerCell]
  

происходит то же самое, ничего, кроме пустой ячейки

изображение, показывающее пустые ячейки и только один вид выбора даты

любая помощь?

Комментарии:

1. В библиотеке это похоже на cells = [[datePickerCell]], поэтому измените, а затем попробуйте. вам не хватает одной скобки

2. даже если бы я сделал это cells = [[datePickerCell] , [datePickerCell]] , ячейки по-прежнему пусты

3. Хорошо, я сделаю для вас одну демонстрацию. Вы хотите, чтобы datepicker был в каждой ячейке, верно?

4. да, я хочу добавлять новую ячейку с помощью DatePicker каждый раз, когда я нажимаю на кнопку.

5. Хорошо, я попробую и отвечу вам @ienamo

Ответ №1:

Просто замените свою функцию кнопки на

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