移动单元格时,TableViewCell高度会发生变化

时间:2018-01-20 19:54:45

标签: ios uitableview core-data height row

我正在使用Autolayout和estimatedHeightForRowAt动态更改单元格高度。这是我正在使用的代码:

var heightAtIndexPath = NSMutableDictionary()

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    let height = self.heightAtIndexPath.object(forKey: indexPath)
    if ((height) != nil) {
        return CGFloat(height as! CGFloat)
    } else {
        return UITableViewAutomaticDimension
    }
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    let height = cell.frame.size.height
    self.heightAtIndexPath.setObject(height, forKey: indexPath as NSCopying)
}

viewDidLoad()

tableView.rowHeight = UITableViewAutomaticDimension
tableView.reloadData()

现在,当我向右滑动以“完成任务”时,对于该特定单元格,数据分类的priorityNumber变为0,并且单元格移动到列表的底部。当coreData发生更改时,我使用NSFetchedResultsController changeType .move自动将单元格移动到底部。

问题在于移动时单元格会稍微改变它的高度。它是随机的 - 有时它会在它进入'0'位置时发生变化,有时它会在它回到原始位置时发生变化。

它总是不会发生,但有时也会发生。知道可能是什么问题。以下是自定义单元格中内容的自动布局约束:

textview.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 48).isActive = true
    textview.topAnchor.constraint(equalTo: contentView.topAnchor,  constant: 6).isActive = true
    textview.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8).isActive = true
    textview.bottomAnchor.constraint(equalTo: scheduledSign.topAnchor, constant: -2).isActive = true
   // textview.bottomAnchor.constraint(equalTo: line.topAnchor, constant: 0).isActive = true

    _ = scheduledSign.anchor(nil, left: contentView.leftAnchor, bottom: line.topAnchor, right: contentView.rightAnchor, topConstant: 2, leftConstant: 54, bottomConstant: 4, rightConstant: 8, widthConstant: 0, heightConstant: 0)

    line.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 48).isActive = true
    line.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 0).isActive = true
    line.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true
   // line.topAnchor.constraint(equalTo: textview.bottomAnchor, constant: 0).isActive = true
    line.heightAnchor.constraint(equalToConstant: 0.5).isActive = true

   _ = prioritycircle.anchor(self.contentView.topAnchor, left: self.contentView.leftAnchor, bottom: nil, right: nil, topConstant: 20, leftConstant: 16, bottomConstant: 0, rightConstant: 8, widthConstant: 20  , heightConstant: 20)

请帮忙!感谢。

0 个答案:

没有答案