单元格中的文本视图具有自动大小

时间:2016-12-16 11:09:45

标签: swift xcode uitableview cell

我正在使用Chatview ViewController的tableview。 我需要大小以适应textView。 高度和宽度。但我不能用这两个参数来制作它。

第一次设置约束,看起来像this。 这第二次设置约束,看起来像this

这是代码MyProfile Cell。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if messages[indexPath.row].image != nil {
        tableView.rowHeight = 200
        if !messages[indexPath.row].owner! {
            let cell = self.chatTableView.dequeueReusableCellWithIdentifier("chatImageCell", forIndexPath: indexPath) as! ChatImageTableViewCell
            cell.imageV.image = messages[indexPath.row].image
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tappedAtImage(_:)))
            cell.imageV.addGestureRecognizer(tapGesture)
            cell.imageV.userInteractionEnabled = true
            cell.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
            return cell
        } else {
            let cell = self.chatTableView.dequeueReusableCellWithIdentifier("chatImageCellEnemy", forIndexPath: indexPath) as! ChatImageTableViewCell
            cell.imageV.image = messages[indexPath.row].image
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tappedAtImage(_:)))
            cell.imageV.addGestureRecognizer(tapGesture)
            cell.imageV.userInteractionEnabled = true
            cell.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
            return cell
        }
    } else if messages[indexPath.row].owner! {
        tableView.rowHeight = UITableViewAutomaticDimension
        let cell = self.chatTableView.dequeueReusableCellWithIdentifier("mymessageCell", forIndexPath: indexPath) as! MyChatTableViewCell
        cell.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))

        cell.textView.text = messages[indexPath.row].text!
        cell.textView.layer.cornerRadius = 8
        cell.textView.layer.masksToBounds = true
        cell.textView.textAlignment = .Right
        cell.textView.textColor = .whiteColor()
        cell.textView.sizeToFit()
        cell.textView.layoutIfNeeded()


        let unix = NSTimeInterval(messages[indexPath.row].unixDate!)
        let messageDate = NSDate(timeIntervalSince1970: unix!)
        cell.timeLabel.text = timeAgoSinceDate(messageDate, numericDates: true)

        return cell

    } else {
        let cell = self.chatTableView.dequeueReusableCellWithIdentifier("messageCell", forIndexPath: indexPath) as! ChatTableViewCell
        tableView.rowHeight = UITableViewAutomaticDimension
        cell.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))

        cell.textView.text = messages[indexPath.row].text!
        cell.textView.layer.cornerRadius = 8
        cell.textView.layer.masksToBounds = true
        cell.textView.textColor = .blackColor()
        cell.textView.sizeToFit()
        cell.textView.layoutIfNeeded()

        let unix = NSTimeInterval(messages[indexPath.row].unixDate!)
        let messageDate = NSDate(timeIntervalSince1970: unix!)
        cell.timeLabel.text = timeAgoSinceDate(messageDate, numericDates: true)

        return cell
    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

阅读此博客,那里给出了精彩的答案。希望这对你有所帮助,如果没有让我,那么我将继续你的代码工作。