Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reload rows without calling tableview.reload

Hello in my application i am appending data into array then inserting rows into table view like this

 self.table.beginUpdates()
 self.datasource.append(Data)
 self.table.insertRows(at: [IndexPath(row: self.datasource.count-1, section: 0)], with: .automatic)
 self.table.endUpdates()

everything is working fine in this code ,now when i want to swipe to refresh the all the rows but i don't want to use table.reloadData because it remove all the records and show white screen for a seconds , i want to reload all the rows without using it i am trying in this way but it crash the application

@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()
        }
    }

application crash on

   self.table.endUpdates()
Thread 1: EXC_BAD_ACCESS (code=1, address=0x20)
like image 471
Shahzad Avatar asked Oct 16 '25 04:10

Shahzad


1 Answers

your approach was right:

you have to also append the new item into your "itemlist"

items.append(newItem)

let selectedIndexPath = IndexPath(row: items.count - 1, section: 0)

tableView.beginUpdates()
tableView.insertRows(at: [selectedIndexPath], with: .automatic)
tableView.endUpdates()

thats all :)


EDIT: Example

a little example which works perfectly for me:

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
    }
}

fyi: .anchor() is a method written by me. - it sets all AutoLayout-Constraints.

like image 94
Sandu Avatar answered Oct 17 '25 19:10

Sandu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!