对不起,我是很新的快捷。 我想在我的阵列添加两个或多个项目animalsList
同时,在我的tableview做一个更新。 我如何分配我的indexPath有两个的indeces? 我试图寻找答案在这里,但我找不到任何。
如何更新表视图,如果我添加更多的商品?
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;
}