【问题标题】:Height and Width of tableViewCell changes on scrolling in swifttableViewCell 的高度和宽度在快速滚动时发生变化
【发布时间】:2020-02-11 14:13:09
【问题描述】:
我正在开发基于套接字的聊天模块。我在一个 tableView 中使用 5 个不同的自定义单元格用于不同的目的。当我滚动 tableView 时,它会更改某些单元格的布局、宽度和高度。请帮帮我。我已经解决了太多 StackOverflow 的问题,但我的问题没有得到解决。
【问题讨论】:
标签:
ios
swift
uitableview
ios-autolayout
【解决方案1】:
如果您在情节提要上设置自动行高或宽度,请取消单击它,可能会更改布局。
在视图控制器中,您可以像这样设置高度;
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {}
【解决方案2】:
我正在使用扩展程序来圆角我的单元格。该扩展导致了问题。我正在使用以下扩展程序。
extension UIView {
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
}
}
为了解决我的问题,我在 tableViewCell 中使用了以下代码
override func prepareForReuse() {
super.prepareForReuse()
self.layoutIfNeeded()
self.chatTextView.clipsToBounds = true
chatTextView.layer.cornerRadius = 10
chatTextView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner, .layerMaxXMinYCorner]
//set the cell to initial state here
//set like the button to initial state - title, font, color, etc.
}
}
现在我的细胞工作正常 :)