我正在使用自动布局和 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,并且该单元格移至列表底部。我正在使用 NSFetchedResultsController
changeType .move
在更改 coreData 时自动将单元格移动到底部。
问题是单元格在移动时会稍微改变它的高度。它是随机的——有时当它到达“第 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)
这里是这个问题的视频:https://youtu.be/ecU3_ticw3g
求助!谢谢。
这是我的cellforrowatindexPath
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellid, for: indexPath) as! TasksTableViewCell
cell.textview.text = fetchedResultsController.object(at: indexPath).sNote
cell.prioritycircle.backgroundColor = fetchedResultsController.object(at: indexPath).sPriorityColor
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.backgroundColor = fetchedResultsController.object(at: indexPath).cellbackgroundColor
cell.textview.backgroundColor = fetchedResultsController.object(at: indexPath).cellbackgroundColor
cell.belliconview.tintColor = fetchedResultsController.object(at: indexPath).belliconcolor
// cell.scheduledSign.backgroundColor = fetchedResultsController.object(at: indexPath).cellbackgroundColor
if (fetchedResultsController.object(at: indexPath).sTaskCompleted)
{
cell.isUserInteractionEnabled = false
}
else{
cell.isUserInteractionEnabled = true
}
if (fetchedResultsController.object(at: indexPath).sReminderState) {
cell.belliconview.tintColor = (fetchedResultsController.object(at: indexPath).belliconcolor)
//cell.scheduledSign.text = fetchedResultsController.object(at: indexPath).sReminderDate
} else {
cell.belliconview.tintColor = (fetchedResultsController.object(at: indexPath).belliconcolor)
}
cell.layer.shouldRasterize = true
cell.layer.rasterizationScale = UIScreen.main.scale
return cell
}
这就是我在 FRC 中的内容更改后重新加载我的 tableview 的地方:
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .delete:
tableView.deleteRows(at: [indexPath!], with: .fade)
case .insert:
tableView.insertRows(at: [newIndexPath!], with: UITableViewRowAnimation.automatic)
case .move:
tableView.deleteRows(at: [indexPath!], with: .fade)
tableView.insertRows(at: [newIndexPath!], with: .fade)
case .update:
tableView.reloadRows(at: [indexPath!], with: .fade)
default:
return
}
}
最佳答案
简单的逻辑。尝试使用一次 UILabel
而不是 UITextView
。
在 UITableViewCell (Row height = 44) 中添加 UILabel。给出约束条件“5,5,5,5”和高度“34”。 行数
为零。在 Size Inspector
中,将 horizontal
和 vertical
分别指定为 1000
和 1000
Content Hugging Property
和Content Compression Resistance Priority
。
点击,UILabel的高度约束,在Size Inspector
中,将其Priority
改为750。
let getValuesInString = ["abc\nLets meet today\nyes. sure", "123", "qwerty\n12345\nASDD\n123"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return getValuesInString.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SampleTableViewCell
cell.testLbl.text = getValuesInString[indexPath.row]
cell.selectionStyle = .none
return cell
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Storyboard约束
关于ios - 自定义 UITableViewCell 高度在移动时发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48479246/