Извините, я очень новичок в Swift. Я пытаюсь добавить два или более элементов в мой массив animalsList
одновременно и выполнить обновление в моем табличном представлении. Как я могу назначить свой indexPath для двух индексов? Я пытался найти ответы здесь, но я не могу найти ни одного.
Как мне обновить табличное представление, если я добавлю больше элементов?
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var animalsList = ["dog", "cat", "pig"];
@IBOutlet weak var tableView: UITableView!
@IBAction func click(_ sender: Any) {
animalsList.append("horse");
animalsList.append("mouse");
let indexPath = IndexPath(row: animalsList.count - 1, section: 0);
tableView.beginUpdates();
tableView.insertRows(at: [indexPath], with: .none);
tableView.endUpdates();
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return animalsList.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell");
cell.textLabel?.text = animalsList[indexPath.row];
return cell;
}