【问题标题】:Resize UITableView Height (embedded inside UIScrollView)调整 UITableView 高度(嵌入在 UIScrollView 中)
【发布时间】:2015-10-08 09:45:37
【问题描述】:

在我的storyboard 中,我使用的是AutoLayout,而我的UITableView 嵌入在UIScrollView 中。 UITableView 的单元格数量在运行时会发生变化,我想动态更改UITableView 的框架高度,并禁用UITableView 中的滚动。 我得到的代码显示了UITableView,但我无法调整高度。

class ViewController: UIViewController , UITableViewDataSource ,UITableViewDelegate  {
@IBOutlet weak var myTableView: UITableView!
var array: [CellData] = [CellData]()
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell : CellKistNames = tableView.dequeueReusableCellWithIdentifier("Cell") as! CellKistNames
    cell.cellSiingle.text = "cellData"
    return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.array.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension

}
override func viewDidAppear(animated: Bool) {
    myTableView.reloadData()
}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.myTableView.estimatedRowHeight = 44.0
    self.myTableView.rowHeight = UITableViewAutomaticDimension

    self.myTableView.dataSource = self
    self.myTableView.delegate = self

    array = [
            CellData(name: "one"),CellData(name: "two"),CellData(name: "three"),CellData(name: "four"),CellData(name: "five"),CellData(name: "six"),CellData(name: "sevens")
            ]
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

【问题讨论】:

    标签: ios swift uitableview uiscrollview autolayout


    【解决方案1】:

    每当用于呈现表格视图的集合发生变化时,更新 UITableView height constrain 也将新高度设置为 [collection Count]*tableViewHeight。

    在 XIB 中有两个属性用于表格视图的水平和垂直滚动,只需取消选中这两个属性即可禁用 UITableView 中的滚动。

    【讨论】:

    • @Mrs. Agarwal....谢谢您的回复..滚动条被禁用但我应该如何更新 UiTableView 高度
    • 你必须为高度约束创建一个属性,比如@property (nonatomic, strong) NSLayoutConstraint *tableViewHeightConstraint;现在将此约束添加到表视图中,例如 [self.tableView addConstraint:self.tableViewHeightConstraint];您可以更新此约束值,例如 self.tableViewHeightConstraint.constant = 3*50;(假设 30 作为集合计数,50 作为表格视图单元格的高度)。在表视图上更新约束值调用“updateConstraints”后,例如 [self.tableView updateConstraints];
    猜你喜欢
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 2011-09-23
    • 2015-07-09
    • 1970-01-01
    • 2012-05-17
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多