简体   繁体   中英

Stop tableview auto scroll when reload

My problem is when I click on plus or minus I reload to tableview that is okay but when it reload it is automatically scroll and also my section header is blink. I have attached clip for more clearance.

And I used this code for reload tableview

let contentOffset = tblItemList.contentOffset
tblItemList.reloadData()
tblItemList.layoutIfNeeded()
tblItemList.setContentOffset(contentOffset, animated: false)

Added

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! ItemCell

    if arrCategoryList.count > 0 {
        let data = (arrCategoryList[indexPath.section].product_list ?? [])[indexPath.row]
        cell.setItemData(data)

        if data.is_added {
            cell.btnAdd.isHidden = true
            cell.lblQuantity.text = "\(data.product_quantity)"
        }
        else {
            cell.btnAdd.isHidden = false
        }

        cell.btnMinus.tag = indexPath.row
        cell.btnPlus.tag = indexPath.row
        cell.btnAdd.tag = indexPath.row

        cell.btnAdd.addTarget(self, action: #selector(onClickAdd), for: .touchUpInside)
        cell.btnPlus.addTarget(self, action: #selector(onClickPlus), for: .touchUpInside)
        cell.btnMinus.addTarget(self, action: #selector(onClickMinus), for: .touchUpInside)
    }
    return cell
}

You are reloading whole tableView for updating a single cell. Just use tableView.reloadRows(at: [IndexPath(row: //row goes here, section: //section goes here)], with: .automatic) and header blinks may be because you using UItableViewCell as header view. Just use UITableHeaderFooterView with the help of XIB and register it as HeaderView in your tableView. Just see this for header

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM