Я создаю приложение чата, в котором я использую автоматическую высоту табличного представления для ячеек, но высота для ячейки не работает должным образом для некоторых ячеек. Я просто добавляю источник данных и делегата в методе viewDidLoad, а затем поступают данныес сервера через alamofire, и когда я получил данные, я вызываю метод reloadData ().И этот код для отображения ячейки с данными здесь это код
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let dates = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let currentDate=formatter.string(from: dates)
let message=self.messagesList[indexPath.row]
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSSSS"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let date = dateFormatter.date(from: message.date!)
let row=indexPath.row
if message.senderDisplayName == "Rana Asad"{
if indexPath.row != 0{
let message0=self.messagesList[indexPath.row-1]
let innerDate=dateFormatter.date(from: message0.date!)
if message0.senderDisplayName == "Rana Asad"{
if innerDate?.toString(withFormat: "HH:mm") == date?.toString(withFormat: "HH:mm"){
let cell = tabelview.dequeueReusableCell(withIdentifier: "ConservationTableViewCell", for: indexPath) as! ConservationTableViewCell
cell.recivingView.isHidden=true
cell.senderView.isHidden=false
cell.senderLabel.text=message.text!
cell.senderLabel.sizeToFit()
cell.recivingLabel.sizeToFit()
return cell
}else{
let cell = tabelview.dequeueReusableCell(withIdentifier: "ConservationSecondTableViewCell", for: indexPath) as! ConservationSecondTableViewCell
cell.receivingView.isHidden=true
cell.senderView.isHidden=false
cell.receivingTimeLabel.isHidden=true
cell.timeLabel.isHidden=false
cell.senderLabel.sizeToFit()
cell.receivingLabel.sizeToFit()
cell.senderLabel.text=message.text!
if innerDate?.toString(withFormat: "yyyy-MM-dd") == date?.toString(withFormat: "yyyy-MM-dd"){
cell.todayLabel.isHidden=true
cell.timeLabel.text=date?.toString(withFormat: "HH:mm")
}else{
if currentDate == date?.toString(withFormat: "yyyy-MM-dd"){
cell.todayLabel.isHidden=false
cell.todayLabel.text="Today"
cell.timeLabel.text=date?.toString(withFormat: "HH:mm")
}else{
cell.todayLabel.isHidden=false
cell.todayLabel.text=date?.toString(withFormat: "MM-dd-yyyy")
cell.timeLabel.text=date?.toString(withFormat: "HH:mm")
}
}
return cell
}
}else{
let cell = tabelview.dequeueReusableCell(withIdentifier: "ConservationSecondTableViewCell", for: indexPath) as! ConservationSecondTableViewCell
cell.receivingView.isHidden=true
cell.senderView.isHidden=false
cell.receivingTimeLabel.isHidden=true
cell.timeLabel.isHidden=false
cell.senderLabel.text=message.text!
cell.senderLabel.sizeToFit()
cell.receivingLabel.sizeToFit()
if innerDate?.toString(withFormat: "yyyy-MM-dd") == date?.toString(withFormat: "yyyy-MM-dd"){
cell.todayLabel.isHidden=true
cell.timeLabel.text=date?.toString(withFormat: "HH:mm")
}else{
if currentDate == date?.toString(withFormat: "yyyy-MM-dd"){
cell.todayLabel.isHidden=false
cell.todayLabel.text="Today"
cell.timeLabel.text=date?.toString(withFormat: "HH:mm")
}else{
cell.todayLabel.isHidden=false
cell.todayLabel.text=date?.toString(withFormat: "MM-dd-yyyy")
cell.timeLabel.text=date?.toString(withFormat: "HH:mm")
}
}
return cell
}
}else{
let cell = tabelview.dequeueReusableCell(withIdentifier: "ConservationSecondTableViewCell", for: indexPath) as! ConservationSecondTableViewCell
cell.receivingView.isHidden=true
cell.senderView.isHidden=false
cell.senderLabel.text=message.text!
cell.receivingTimeLabel.isHidden=true
cell.timeLabel.isHidden=false
cell.senderLabel.sizeToFit()
cell.receivingLabel.sizeToFit()
if currentDate == date?.toString(withFormat: "yyyy-MM-dd"){
cell.todayLabel.isHidden=false
cell.todayLabel.text="Today"
cell.timeLabel.text=date?.toString(withFormat: "HH:mm")
}else{
cell.todayLabel.isHidden=false
cell.todayLabel.text=date?.toString(withFormat: "MM-dd-yyyy")
cell.timeLabel.text=date?.toString(withFormat: "HH:mm")
}
return cell
}
}else{
let cell = tabelview.dequeueReusableCell(withIdentifier: "ConservationTableViewCell", for: indexPath) as! ConservationTableViewCell
cell.senderView.isHidden=true
cell.recivingLabel.isHidden=true
cell.layoutIfNeeded()
return cell
}
}
, а вот ограничения вида
вот снимок экрана иерархии представлений