I need to increase UITableView
height based on UITableViewCell
content size.
I'm using Custom Google Auto Complete. I have an UITextField
. When I enter a letter in that UITextField
it will call shouldChangeCharactersIn
range delegate method.
Inside that method I will send dynamic request to Google AutoComplete API to get predictions result.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if tableView.numberOfRows(inSection: 0) > 0
{
let newLength = (textField.text?.characters.count)! + string.characters.count - range.length
let enteredString = (textField.text! as NSString).replacingCharacters(in: range, with:string)
if newLength == 0 {
tableView.isHidden = true
}
if newLength > 0
{
address.removeAll()
self.tableView.isHidden = false
GetMethod("https://maps.googleapis.com/maps/api/place/autocomplete/json?input=\(enteredString)&key=MYAPIKEY", completion: { (resultDict) in
let resultArray = resultDict.object(forKey: "predictions")! as! NSArray
print(resultArray.count)
for temp in resultArray
{
let newtemp = temp as! NSDictionary
let tempAdd = newtemp.value(forKey:"description") as! String
let placeId = newtemp.value(forKey:"place_id") as! String
var dict = [String : AnyObject]()
dict["address"] = tempAdd as AnyObject?
dict["latlong"] = placeId as AnyObject?
self.address.append(dict as AnyObject)
print(newtemp.value(forKey:"description"))
print(self.address.count)
self.tableView.reloadData()
}
})
}
return true
}
After I will store all address to Address array dynamically, I need to increase UITableView
height based on that incoming address content.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "TableViewCell"
var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
if cell == nil
{
cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentifier)
}
let addresstoDisp = address[indexPath.row] as! NSDictionary
let name = addresstoDisp.value(forKey: "address")
cell?.textLabel?.numberOfLines = 0
cell?.translatesAutoresizingMaskIntoConstraints = false
cell?.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
cell?.textLabel?.textAlignment = NSTextAlignment.center
cell?.textLabel?.text = name as! String
return cell!
}
UITableViewCell
height is increasing perfectly. Also I need to increase tableView height.
To change the height of tableView cell in ios dynamically, i.e resizing the cell according to the content available, we'll need to make use of automatic dimension property. We'll see this with the help of an sample project.
Add these lines after your cells are created. Because it returns 0 height in viewDidLoad()
var frame = tableView.frame
frame.size.height = tableView.contentSize.height
tableView.frame = frame
UPDATE
//After Tableviews data source values are assigned
tableView.reloadData()
tableView.layoutIfNeeded()
tableView.heightAnchor.constraint(equalToConstant: tableView.contentSize.height).isActive = true
The below code worked for me with UITableViewCell who has AutomaticDimension.
- Create an outlet for tableViewHeight.
@IBOutlet weak var tableViewHeight: NSLayoutConstraint!
var tableViewHeight: CGFloat = 0 // Dynamically calcualted height of TableView.
- For the dynamic height of the cell, I used the below code:
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return tableView.estimatedRowHeight
}
- For height of the TableView, with dynamic heights of the TableView Cells:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
print(cell.frame.size.height, self.tableViewHeight)
self.tableViewHeight += cell.frame.size.height
tableViewBillsHeight.constant = self.tableViewHeight
}
Explanation:
After the TableView cell is created, we fetch the frame height of the cell that is about to Display and add the height of the cell to the main TableView.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With