1
在我的storyboard
中,我使用的是AutoLayout
,我的UITableView
嵌入在UIScrollView
的內部。 UITableView
的單元格數量在運行時發生變化,我想動態更改我的UITableView
幀的高度,並禁用UITableView
中的滾動。 我得到的代碼顯示UITableView
,但我無法調整高度。調整UITableView高度(嵌入在UIScrollView中)
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.
}
}
@ Mrs. Agarwal ....謝謝你的回覆..滾動被禁用,但我應該如何更新UiTableView的高度 – Cloy
你必須創建一個屬性來限制高度,比如@property(nonatomic,strong)NSLayoutConstraint * tableViewHeightConstraint;現在將這個約束添加到表視圖中,如[self.tableView addConstraint:self.tableViewHeightConstraint];您可以更新此約束值,如self.tableViewHeightConstraint.constant = 3 * 50;(假設30爲收集計數,50爲表格視圖單元格的高度)。在更新表視圖上的約束值調用「updateConstraints」後,如[self.tableView updateConstraints]; –
謝謝@Mrs。 Agrawal ....你的解決方案很有幫助... – Cloy