根据单元格增加TableView的高度 [英] increase TableView height based on cells

查看:46
本文介绍了根据单元格增加TableView的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据UITableViewCell内容的大小增加UITableView的高度. 我正在使用自定义Google自动完成功能.我有一个UITextField.当我在UITextField中输入字母时,它将调用shouldChangeCharactersIn范围委托方法.

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.

在该方法中,我将向Google AutoComplete API发送动态请求以获取预测结果.

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
}

将所有地址动态存储到地址数组之后,需要根据传入的地址内容增加UITableView的高度.

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高度正在完美增加.另外,我需要增加tableView的高度.

UITableViewCell height is increasing perfectly. Also I need to increase tableView height.

推荐答案

在创建单元格后添加以下行.因为它在viewDidLoad()中返回0高度

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

更新

//After Tableviews data source values are assigned
tableView.reloadData()
tableView.layoutIfNeeded()
tableView.heightAnchor.constraint(equalToConstant: tableView.contentSize.height).isActive = true

这篇关于根据单元格增加TableView的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆