在uitableview swift中同时添加两行 [英] Adding two rows simultaneously in uitableview swift
本文介绍了在uitableview swift中同时添加两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
抱歉,我对 swift 很陌生.我正在尝试同时在我的数组 animalsList
中添加两个或多个项目,并在我的 tableview 中进行更新.如何分配我的 indexPath 有两个 indeces?我试图在这里寻找答案,但找不到任何答案.
Sorry, I am very new to swift. I am trying to add two or more items in my array animalsList
simultaneously and do an update in my tableview. How can I assign my indexPath to have two indeces? I tried to look for answers here but I can't find any.
如果添加更多项目,如何更新表格视图?
How can I update the table view if I add more items?
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;
}
推荐答案
要在 UITableView
中同时插入两行,你可以这样做.
For insert two rows simultaneously in UITableView
you can do like this.
@IBAction func click(_ sender: Any) {
animalsList.append("horse")
animalsList.append("mouse")
var Arr_indexPath = [IndexPath]()
Arr_indexPath.append(IndexPath(row: animalsList.index(of: "horse")!, section: 0))
Arr_indexPath.append(IndexPath(row: animalsList.index(of: "mouse")!, section: 0))
tableView.insertRows(at: Arr_indexPath, with: .none)
}
这篇关于在uitableview swift中同时添加两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文