【问题标题】:uitableview height does not change dynamically in swift ios?uitableview 高度不会在 swift ios 中动态变化?
【发布时间】:2014-12-12 07:29:51
【问题描述】:

我遇到了麻烦。我有一个城市列表,我想在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
    }
}

在上述代码的预览下方。

【问题讨论】:

  • 您需要添加约束来更改表格视图的动态高度。看到这个答案 - stackoverflow.com/questions/27414052/…
  • @Kampai 你能建议如何将 NSLayoutConstraint 变量与高度约束联系起来吗?

标签: ios uitableview swift


【解决方案1】:

由于此答案所需的解释非常详细。

按照我的其他回答 "Moving views with constraints" 了解如何更新布局约束。

根据您对如何将 NSLayoutConstraint 变量与高度约束联系起来的评论? 如下:

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

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

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

【讨论】:

  • 当我们在 UITableView 上应用高度限制时,它不显示模拟器。
  • 最初高度约束的常数值是多少?我的意思是在情节提要约束属性中。
【解决方案2】:

如果您的所有单元格都具有相同的外观,您可以使用原型单元格:

a complete tutorial to make a project using prototype cell

a second which it more quick to read

你也可以用

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
  return 42.0
}

如果您动态调整高度单元格的大小,此功能非常完美。

你只需要使用:beginUpdates()endUpdates() 就可以了。

【讨论】:

  • 我想增加 UITableView 高度,而不是 UITableView 单元格高度。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-30
  • 2023-03-30
  • 1970-01-01
  • 2019-11-12
  • 2012-12-22
  • 2013-04-09
相关资源
最近更新 更多