Ios 重新加载行而不调用tableview.reload
您好,在我的应用程序中,我将数据追加到数组中,然后像这样将行插入表视图中Ios 重新加载行而不调用tableview.reload,ios,swift,uitableview,Ios,Swift,Uitableview,您好,在我的应用程序中,我将数据追加到数组中,然后像这样将行插入表视图中 self.table.beginUpdates() self.datasource.append(Data) self.table.insertRows(at: [IndexPath(row: self.datasource.count-1, section: 0)], with: .automatic) self.table.endUpdates() 在这段代码中一切正常,现在当我想滑动刷新所有行,但我不想使用
self.table.beginUpdates()
self.datasource.append(Data)
self.table.insertRows(at: [IndexPath(row: self.datasource.count-1, section: 0)], with: .automatic)
self.table.endUpdates()
在这段代码中一切正常,现在当我想滑动刷新所有行,但我不想使用table.reloadData时,因为它会删除所有记录并在几秒钟内显示白色屏幕,所以我想在不使用它的情况下重新加载所有行
我正以这种方式尝试,但它会使应用程序崩溃
@objc func handleRefresh(_ refreshControl: UIRefreshControl) {
if (datasource.count > 0 ){
datasource.removeAll()
page_num = 1
get_past_orders(page: page_num)
self.isLoading = false
refreshControl.endRefreshing()
}
}
应用程序崩溃
self.table.endUpdates()
Thread 1: EXC_BAD_ACCESS (code=1, address=0x20)
你的方法是正确的: 您还必须将新项目附加到“项目列表”中 仅此而已:)
编辑:示例 一个非常适合我的小例子:
class ViewController: UIViewController {
struct Item {
var field_a: String
var field_b: Bool
var field_c: Int
}
// MARK: - properties
var items = [Item]()
// MARK: - object-properties
let tableView = UITableView()
let addButton = UIButton()
// MARK: - system-methods
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.addSubview(addButton)
addButton.titleLabel?.text = "ADD"
addButton.titleLabel?.textColor = .white
addButton.backgroundColor = .blue
addButton.addTarget(self, action: #selector(handleButton), for: .touchUpInside)
tableView.dataSource = self
addButton.anchor(top: nil, leading: nil, bottom: view.bottomAnchor, trailing: view.trailingAnchor, padding: UIEdgeInsets(top: 0, left: 0, bottom: 24, right: 24), size: CGSize(width: 84, height: 48))
tableView.anchor(top: view.topAnchor, leading: view.leadingAnchor, bottom: view.bottomAnchor, trailing: view.trailingAnchor)
prepareItems()
}
// MARK: - preparation-methods
func prepareItems() {
items.append(Item(field_a: "cell1", field_b: false, field_c: 0))
items.append(Item(field_a: "cell2", field_b: false, field_c: 0))
items.append(Item(field_a: "cell3", field_b: false, field_c: 0))
}
// MARK: - helper-methods
func appendNewItem(_ item: Item) {
items.append(item)
let selectedIndexPath = IndexPath(row: items.count - 1, section: 0)
tableView.beginUpdates()
tableView.insertRows(at: [selectedIndexPath], with: .automatic)
tableView.endUpdates()
}
// MARK: - action-methods
@objc func handleButton() {
appendNewItem(Item(field_a: "new Cell", field_b: true, field_c: 0))
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let currItem = items[indexPath.row]
cell.textLabel?.text = currItem.field_a
return cell
}
}
仅供参考:.anchor()是我编写的一个方法。-它设置了所有的自动布局约束。如果您想重新加载tableView的行,而不每次调用tableView的重新加载方法。 你可以用
self.itemTableView.reloadRows(at: [NSIndexPath(row: 0, section: 0) as IndexPath], with: .automatic)
您可以在指定位置插入所需的IndexPath值(要重新加载)
[NSIndexPath(row: 0, section: 0) as IndexPath]
并且:这里是UITableViewRowAnimation类型,您希望用动画重新加载。它提供了许多选项,如.automatic、.bottom、.fade、.middle、.top、.left、.right和.none等
所有这些动画选项都为您提供了不同的方式来重新加载具有不同类型的表行。使用Objective-C:
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathOfYourCell, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
我需要在刷卡刷新后或每次?每次获得新数据时都要这样做。在您的逻辑中,我认为您必须在刷卡刷新完成后调用它。仅供参考,
.insertRows
仅使用您在insertRows
@Shahzad的第一个参数中转发的索引来更新所有单元格:“它不起作用”对您没有多大帮助。请指出哪个部分不起作用。begin-/endUpdates
对单个insert/delete/move
操作完全无效。它仍在崩溃。你说滑动刷新是什么意思,我只见过拉动刷新!是的,我说的是拉动刷新!不需要做任何事情重新加载工作完成我不想使用重新加载,因为它在订单加载时会出现几秒钟的白色屏幕,但我不知道为什么它会使我在最后分页时遇到问题,它会再次刷新
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathOfYourCell, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];