自定义UITableViewCell高度在移动时发生变化 [英] Custom UITableViewCell height changes when moved

查看:64
本文介绍了自定义UITableViewCell高度在移动时发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自动布局和estimatedHeightForRowAt动态更改像元高度.这是我正在使用的代码:

I am using Autolayout and estimatedHeightForRowAtto dynamically change cell height. This is the code I am using:

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将单元格自动移至底部.

Now when I do a swipe right to 'complete a task', the priorityNumberon which the data is sorted becomes 0 for that particular cell and the cell moves to the bottom of the list. I am using NSFetchedResultsController changeType .move to move the cell to the bottom automatically when the coreData is changed.

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

The issue is that the cell changes it's height a bit when moved. It's random - sometimes it changes when it goes to the '0th' position and sometimes it changes when it goes back to it's original position.

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

It doesn't happen always, but sometimes. Any idea what maybe the issue. Here is the autolayout constraints for the contents in the custom cell:

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

Here is a video of this issue: 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中更改内容后重新加载表格视图的地方:

and this is where I am reloading my tableview after content change in FRC:

 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.

Simple Logic. Try to use UILabel instead of UITextView once.

在UITableViewCell中添加UILabel(行高= 44).给出约束"5,5,5,5"和高度为"34". Number of lines为零.在Size Inspector中,将horizontalvertical命名为1000& 1000Content Compression Resistance Priority分别为1000.

Add UILabel in UITableViewCell (Row height = 44) . Give constraints, "5,5,5,5" and height as "34". Number of lines as Zero. In Size Inspector, Give horizontal and vertical as 1000 & 1000 respectively for both Content Hugging Property and Content Compression Resistance Priority.

单击UILabel的高度约束,在Size Inspector中,将其Priority更改为750.

Click, UILabel's Height Constraints, in Size Inspector, Change its Priority to 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 

}

故事板约束

这篇关于自定义UITableViewCell高度在移动时发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆