【问题标题】:Stop tableview auto scroll when reload重新加载时停止tableview自动滚动
【发布时间】:2020-05-25 17:59:31
【问题描述】:

我的问题是,当我单击加号或减号时,我重新加载到 tableview 没问题,但是当它重新加载时,它会自动滚动,而且我的部分标题也会闪烁。我已附上clip 以获得更多许可。

我使用这段代码重新加载表格视图

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

添加

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
}

【问题讨论】:

  • 你能分享cellForRowAt的逻辑吗?我也认为reloadData 足以刷新数据。如果您的身高计算/估计正确,则不需要最后两行。
  • 尝试删除tblItemList.layoutIfNeeded(),看看是否是问题的原因。另外,需要tblItemList.layoutIfNeeded()吗?
  • @Cerlin 请检查我为行添加了单元格
  • @AhmadF 让我试试这个
  • 我尝试删除 tblItemList.layoutIfNeeded() 但没有工作。它会向下滚动我的表格视图@AhmadF

标签: ios swift iphone uitableview


【解决方案1】:

您正在重新加载整个 tableView 以更新单个单元格。只需使用 tableView.reloadRows(at: [IndexPath(row: //row 到这里,section: //section 到这里)],with: .automatic) 标题闪烁可能是因为您使用 UItableViewCell 作为标题视图。只需在 XIB 的帮助下使用 UITableHeaderFooterView 并将其注册为 HeaderView 在您的 tableView 中。Just see this for header

【讨论】:

  • 感谢您的帮助。但是 UITableHeaderFooterView 这已被弃用。
  • 嗨,kamlesh,看看吧。它可从 ios-6 获得,直到最新更新。也许你生活在未来,你看到它在那里被弃用了:D
猜你喜欢
  • 2021-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-24
  • 2016-08-04
  • 1970-01-01
  • 1970-01-01
  • 2019-02-06
相关资源
最近更新 更多