代码之家  ›  专栏  ›  技术社区  ›  maral

当单元格高度更改时,UITableView会更改滚动位置

  •  -1
  • maral  · 技术社区  · 6 年前

    我有一个 tableView 桌面视图 卷轴非常高或更低 selectedCell

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if cellsMode[indexPath.row] {
            return 344
        }
    else {
            return 140 }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
        self.cellsMode[indexPath.row] = !self.cellsMode[indexPath.row] 
        table.reloadData() 
    } 
    

    let indexPath = NSIndexPath(row: cellInex, section: 0)
    self.table.scrollToRow(at: indexPath as IndexPath, at: .bottom, animated: false) 
    

    但它不起作用。 我怎样才能修好它?

    1 回复  |  直到 6 年前
        1
  •  2
  •   SPatel mash    6 年前

    只需重新加载单元格而不是整个tableview,如下所示:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
        self.cellsMode[indexPath.row] = !self.cellsMode[indexPath.row] 
        tableView.reloadRows(at: [indexPath], with: .automatic)
    }