【发布时间】: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