我正在使用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)
以下是此问题的视频: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
}
}
答案 0 :(得分:0)
简单逻辑。尝试使用UILabel
代替UITextView
一次。
在UITableViewCell中添加UILabel(行高= 44)。给予约束,&#34; 5,5,5,5&#34;和高度为&#34; 34&#34;。 Number of lines
为零。在Size Inspector
中,将horizontal
和vertical
设为1000
&amp; 1000
和Content Hugging Property
分别为Content Compression Resistance Priority
。
在Size Inspector
中点击,UILabel的高度限制,将其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
}
故事板限制