简体   繁体   English

重新加载时停止 tableview 自动滚动

[英]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.我的问题是当我点击加号或减号时,我重新加载到 tableview 是可以的,但是当它重新加载时它会自动滚动并且我的部分标题也会闪烁。 I have attached clip for more clearance.我附上了夹子以获得更多间隙。

And I used this code for reload tableview我使用此代码重新加载 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.您正在重新加载整个 tableView 以更新单个单元格。 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.只需使用tableView.reloadRows(at: [IndexPath(row: //row go here, section: //section go here)], with: .automatic)并且标题闪烁可能是因为您使用 UItableViewCell 作为标题视图。 Just use UITableHeaderFooterView with the help of XIB and register it as HeaderView in your tableView.只需在 XIB 的帮助下使用 UITableHeaderFooterView 并将其注册为 HeaderView 在您的 tableView 中。 Just see this for header 只看这个标题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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