-1

I wrote such extension to display my data in UITableview. But sometimes my data can contain more than 1 line and I need to create something to display full content. How could I change my code (below) to do it?

extension ViewController: UITableViewDataSource, UITableViewDelegate {

    // Define no of rows in your tableView
    func tableView(_ chatHistoryTable: UITableView, numberOfRowsInSection section: Int) -> Int {
        return messagesData.count
    }

    func tableView(_ chatHistoryTable: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = chatHistoryTable.dequeueReusableCell(withIdentifier: "userMessage")! as UITableViewCell

        cell.textLabel!.text = messagesData[indexPath.row]
        cell.textLabel!.textAlignment = .right

        return cell;
    }

}

I think that I should write something for UITableViewCell too, but I don't know, am I correct.

Please help me with this question.

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
Jane Doev
  • 1
  • 4

2 Answers2

0

in here you need to follow two changes.

  • initially set the estimate height and auntomaticdimension for Self Sizing Cell, on your page loads call the following line.

      tableView.estimatedRowHeight = 44.0
      tableView.rowHeight = UITableView.automaticDimension
    

    for e.g

     @IBOutlet weak var yourTableviewName: UITableView!{
    didSet{
        yourTableviewName.tableFooterView = UIView()
        yourTableviewName.estimatedRowHeight = 44.0
       yourTableviewName.rowHeight = UITableView.automaticDimension
        }
      }
    
  • secondary for your word wrap and numberOfLines for your lables. follow the below line on your cellforRow method

       func tableView(_ chatHistoryTable: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = chatHistoryTable.dequeueReusableCell(withIdentifier: "userMessage")! as UITableViewCell
         cell.textLabel!.numberOfLines = 0
        cell.textLabel!.lineBreakMode = .byWordWrapping
        cell.textLabel!.text = messagesData[indexPath.row]
        cell.textLabel!.textAlignment = .right
    
        return cell;
    }
    
    }
    
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
-1

Step 1. Add a chain of unbroken vertical constraints in Custom Table View Cell

Step 2. Set the cell estimated height

tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension

Step 3:Make the label support multiple lines

textLabel.LineBreakMode = UILineBreakMode.WordWrap;
textLabel.Lines = 0;
Pooja Kamath
  • 1,290
  • 1
  • 10
  • 17