表格视图单元格的自动高度无法正常工作

时间:2019-04-29 10:19:30

标签: uitableview nslayoutconstraint uitableviewautomaticdimension

我正在构建聊天应用程序,其中我使用表格视图的自动高度作为单元格,但是单元格的高度对于某些单元格无法正常工作。我只是在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
    }



}

enter image description here

这是视图enter image description here

的约束

这是视图层次结构的屏幕快照 enter image description here

2 个答案:

答案 0 :(得分:0)

我认为您应该为标签高度添加greaterThanOrEqualTo约束

答案 1 :(得分:0)

您需要将contentcompressionresistance(垂直)设置为1000,将contenthuggingpriority(垂直)设置为1000