0

I am using tableview for Chat ViewController. I need size to fit textView. Height and width. But i can't make it with both parameters.

This first time setting constrains and it look like this. And this second time setting constrains and it look like this.

This is code 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
    }
}

Thank you!

coolfrut
  • 1
  • 1

1 Answers1

0

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

Read this blog, fabulous answer is given there. Hope this helps you and if not let me then i will go with your code work.

Community
  • 1
  • 1
dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
  • Thanks for the answer, but I did not understand exactly what I did wrong. Could you help me with the code? – coolfrut Dec 16 '16 at 15:51
  • What problem actually you are facing, the image you have added in your ques , right side of the image i see the output , it appears correct, but your code needs far much correction or may be it is your need so you add all stuff. for info what is `messages[]`. – dahiya_boy Dec 16 '16 at 18:12
  • messages is array of struct Message, so message is: struct Message { var text: String? var unixDate: String? var status: Int? var owner: Bool? var id: String? var image: UIImage? } – coolfrut Dec 16 '16 at 18:32
  • What and where you are facing the problem. I mean what your expecting output and what output you are getting. Please describe again in your question with multiple images. – dahiya_boy Dec 16 '16 at 18:37
  • So main problem is that I can't set automatically with and height. (Now only width or height) – coolfrut Dec 16 '16 at 19:11
  • Ok, give some comments in your code work, so that I can understand what you have done in each line. According to which I will check. And little bit describe what Component you are using like `UILabel` or any other. – dahiya_boy Dec 17 '16 at 05:15
  • I am using a 4 cells. 1 - A Text message by sender, 2 - A Text message by recipient, 3 - Image message by sender, 4 - Image message by recipient. You can see this on screens. So I'm using a rotated tableview and rotated cells for chat. And that's all. All text in textview filling with array messages. It is struct array. – coolfrut Dec 17 '16 at 12:42