There are multiple tableviews in my app, but the tableview with fixed cell size have no problem. However, 2 tableviews that I am using have variable cell size and it is causing lag/stuttering(when the cells with greater height value appears). I initially thought thats maybe because my cell is designed very inefficiently. However even simple table view cells with just a textfield(variable height) and a label(fixed height) is causing stuttering.
Layout constraints of the cell:
1. Label is given a height os 20, pinned 0 from the bottom with 5px trailing and leading
2. TextView is pinned 0 from top and 0 to the label below it with 5px trailing and leading
I have tried everything and looked at many articles but I am not able to find any helpful solution. Here is the cell file:
import UIKit
class ConvoCell: UITableViewCell {
@IBOutlet weak var messageView: UITextView!
@IBOutlet weak var BgViewRightContraint: NSLayoutConstraint!
@IBOutlet weak var BgViewLeftContraint: NSLayoutConstraint!
@IBOutlet weak var TimingInfo: UILabel!
@IBOutlet weak var bgView: UIView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func datainit(message: String, time: String, type: Int32){
self.messageView.text = message
self.TimingInfo.text = time
if (type == 1){
bgView.backgroundColor = .orange
self.messageView.textColor = .white
BgViewRightContraint.constant = 5
BgViewLeftContraint.constant = 50
TimingInfo.textColor = .white
} else {
bgView.backgroundColor = .white
self.messageView.textColor = .black
self.TimingInfo.textColor = .black
BgViewRightContraint.constant = 50
BgViewLeftContraint.constant = 5
}
}
}
Here is the code for my cellForRowAt:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "messageBubble") as? ConvoCell else {
return UITableViewCell()
}
cell.selectionStyle = .none
messagetable.separatorStyle = .none
var currentMessage: Message!
currentMessage = chat.fetchedMessages[indexPath.row]
cell.datainit(message: currentMessage.body!, time: currentMessage.timingInfo!, type: currentMessage.type)
return cell
}
In viewdidLoad(), i have added the estimated rowHeight and the automatic dimensions as well:
messagetable.estimatedRowHeight = 185
messagetable.rowHeight = UITableView.automaticDimension