2016-10-22 2 views
1

Я использую Swift 3. У меня есть UITableView с 7 строк:Добавьте дополнительные строки в TableView

override func viewDidLoad() { 
     super.viewDidLoad()   
     self.tableView.delegate = self 
     self.tableView.dataSource = self   
} 

func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int 
    {   
     return 7 
    } 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    if (IndexPath.row == 6) 
    { 
     tableView.beginUpdates() 
     var index = IndexPath(row: 7, section: 0) 
     tableView.insertRows(at: [index], with: UITableViewRowAnimation.automatic)      
     tableView.endUpdates() 
    } 
} 

Я хочу, чтобы добавить дополнительные ячейки, когда я прокрутку в последнюю ячейку в таблице. Но я получил эту ошибку:

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (7) must be equal to the number of rows contained in that section before the update (7), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).

ответ

1

Вы должны обновить источник данных при вставке или удалить любые строки из Tableview, ГНС, что число объектов сохраняется равное количеству строк в Tableview

func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int {   
    return dataSource.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if (IndexPath.row == 6) { 
     tableView.beginUpdates() 
     var index = IndexPath(row: 7, section: 0) 
     let newObject = // create new model object here 
     dataSource.insert(newObject, at: 7) 
     tableView.insertRows(at: [index], with: UITableViewRowAnimation.automatic)      
     tableView.endUpdates() 
    } 
} 
+0

спасибо! оно работает – Kirill

Смежные вопросы