gpt4 book ai didi

ios - 根据单元格增加 TableView 高度

转载 作者:搜寻专家 更新时间:2023-10-30 21:49:52 25 4
gpt4 key购买 nike

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

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

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
}

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

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 的高度。

最佳答案

在创建单元格后添加这些行。因为它在 viewDidLoad() 中返回 0 高度

 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

关于ios - 根据单元格增加 TableView 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41094672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com