2015-10-08 37 views
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. 
} 
} 

回答

1

每當這是使用渲染表視圖收集得到改變,更新的UITableView 高度限制也有新高度[收集計數] * tableViewHeight。

在XIB中,有兩個用於水平和垂直滾動表格視圖的屬性,只需取消選中它們即可禁用UITableView中的滾動。

+0

@ Mrs. Agarwal ....謝謝你的回覆..滾動被禁用,但我應該如何更新UiTableView的高度 – Cloy

+0

你必須創建一個屬性來限制高度,比如@property(nonatomic,strong)NSLayoutConstraint * tableViewHeightConstraint;現在將這個約束添加到表視圖中,如[self.tableView addConstraint:self.tableViewHeightConstraint];您可以更新此約束值,如self.tableViewHeightConstraint.constant = 3 * 50;(假設30爲收集計數,50爲表格視圖單元格的高度)。在更新表視圖上的約束值調用「updateConstraints」後,如[self.tableView updateConstraints]; –

+0

謝謝@Mrs。 Agrawal ....你的解決方案很有幫助... – Cloy

相關問題