2014-11-02 219 views
0

我有一個UIScrollView,頂部有一些信息來源,並在按鈕上有一個UITableView。表視圖不能滾動,所以爲了能夠顯示大量的單元格,我需要手動計算東西。這就是我實際做的(你可以在下面的代碼中看到)。同時,表格視圖的容器也應隨之增長。我試圖在加載視圖時更改這些參數,但沒有什麼真正改變...動態表格視圖+滾動視圖

這是它的樣子。在IB。

enter image description here

的結構。

enter image description here

和代碼。

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 
    @IBOutlet var scrollView: UIScrollView! 
    @IBOutlet var tableView: UITableView! 

    let numberOfCells = 55 
    let rowHeight: CGFloat = 40 
    let footerHeight: CGFloat = 30 
    let headerHeight: CGFloat = 30 

    let identifier = "Cell" 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.tableView.dataSource = self 
     self.tableView.delegate = self 
     self.tableView.backgroundColor = self.view.backgroundColor 
     self.tableView.scrollEnabled = false 

     self.automaticallyAdjustsScrollViewInsets = false 
     self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: self.identifier) 

     let estimatedHeight = CGFloat(self.numberOfCells) * self.rowHeight + self.footerHeight + self.headerHeight 
     self.tableView.frame.size = CGSizeMake(self.tableView.bounds.width, estimatedHeight) 
     self.scrollView.contentSize = CGSizeMake(self.view.bounds.width, CGRectGetMaxY(self.tableView.frame)) 
     self.scrollView.frame.size = CGSizeMake(self.view.bounds.width, CGRectGetMaxY(self.tableView.frame)) 
    } 

    // MARK:- Table view data source 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.numberOfCells 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier(self.identifier) as UITableViewCell 

     cell.textLabel?.text = "City" 
     cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator 

     return cell 
    } 

    // MARK:- Table view delegate 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     tableView.deselectRowAtIndexPath(indexPath, animated: true) 
    } 

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return self.rowHeight 
    } 

    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
     return self.headerHeight 
    } 

    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
     return self.footerHeight 
    } 
} 

那麼,可能是什麼原因?如何製作我想要的?坦率地說,我對UIScrollView並不很熟悉,但是有一些教程構建了圖像。

回答

1

基本上,這是一個很不好的方法。只有當你有一個非常特殊的功能時才能應用它。一般來說,你可以簡單地把所有內容放在UITableView之內。只需創建一個UIView實例,添加子視圖,製作佈局內容並將此視圖分配爲tableView.tableHeaderView。它適合我,也希望你也能! :)

0

UITableView將自動滾動 - 但它不能,因爲您已將其高度設置爲所有單元格的高度。將高度設置爲屏幕高度,一切都會很好。

UIScrollView是不必要的,但您在這裏也完成了相同的操作 - 您已將滾動視圖框架高度和內容高度設置爲表格視圖的高度。只設置內容高度將使滾動視圖能夠完成其工作。

快樂編碼。

+0

謝謝你的回覆!重點是做一個不可滾動的表格視圖,但提供所有的單元格的幫助下UIScrollView ... – Majotron 2014-11-02 16:54:53