gpt4 book ai didi

ios - uitableview 高度不会在 swift ios 中动态变化?

转载 作者:可可西里 更新时间:2023-11-01 00:17:29 24 4
gpt4 key购买 nike

我有麻烦了。我有一个城市列表,我想在 UITableView 中显示城市,我禁用了 UITableView 的滚动,因为我不想显示滚动。

当我动态改变 UITableView 的高度时,它没有改变,UITableView 没有显示所有城市。在代码下方。请看。

import UIKit

class FlightDetailsOneWayViewController: UIViewController {

var flightDetailArr:[FlightDetailsOneWay] = [FlightDetailsOneWay]()

@IBOutlet var tableView: UITableView!

@IBOutlet var scrollview: UIScrollView!

override func viewDidLoad() {
super.viewDidLoad()

flightDetailArr.append(FlightDetailsOneWay(depCity: "London"))

flightDetailArr.append(FlightDetailsOneWay(depCity: "China"))

flightDetailArr.append(FlightDetailsOneWay(depCity: "Singapore"))

flightDetailArr.append(FlightDetailsOneWay(depCity: "Dubai"))


self.tableView.scrollEnabled = false

var cellHeight = CGFloat(self.tableView.rowHeight)

var totalCity = CGFloat(flightDetailArr.count)

var totalHeight = cellHeight * totalCity

self.tableView.frame.size.height = CGFloat(totalHeight)


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return flightDetailArr.count
}


func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let cell:FlightDetailsCell = tableView.dequeueReusableCellWithIdentifier("FlightDetail") as FlightDetailsCell
let flight = flightDetailArr[indexPath.row]
cell.setCell(flight.depCity)
return cell
}
}

class FlightDetailsOneWay
{
var depCity = ""
init(depCity: String)
{
self.depCity = depCity
}
}


class FlightDetailsCell: UITableViewCell {


@IBOutlet var depCity: UILabel!


override func awakeFromNib()
{
super.awakeFromNib()
// Initialization code
}

override func setSelected(selected: Bool, animated: Bool)
{
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}

func setCell(depCity: String)
{
self.depCity.text = depCity
}
}

在上述代码的预览下方。

preview

最佳答案

因为这个答案需要的解释非常详细。

关注我的另一个回答 "Moving views with constraints" 了解如何更新布局约束。

根据您关于如何将 NSLayoutConstraint 变量与高度约束连接起来的评论?,如下所示:

  1. 转到 Storyboard。
  2. 选择您已添加约束的 UIView。单击约束 图标可查看所有约束。现在在下图中我只添加了一个约束,你可能会发现更多。检查您更改了哪个约束的属性。

enter image description here

  1. 现在点击view controller 并点击Show the connection inspector。在这里你可以看到在你的 View Controller 中定义的所有 IBOutlets。如下图所示,为了简单起见,我只创建了一个 IBOutlet,即 heightConstraint

enter image description here

  1. 这就是您可以将连接添加到约束变量的方法。

enter image description here

关于ios - uitableview 高度不会在 swift ios 中动态变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27438831/

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