簡體   English   中英

在啟用分頁的情況下將tableview添加到scrollview。

[英]Adding tableview to scrollview with paging enabled.

我正在創建一個應用程序,其屏幕之一需要具有帶分頁的UIScrollView。 在scrollview的每個頁面中,需要有一個UITableView,如下所示:

在此處輸入圖片說明

我已經完成了創建啟用頁面調度的scrollview(以編程方式)的工作,但是我面臨的問題是:如果我創建一個簡單的UILabel並將其添加到頁面中,則可以正常工作。 但是,如果我創建一個tableview並將其添加到頁面中(如果這個術語有意義的話),屏幕上將不會顯示任何內容。 這是我的代碼,其中包含有關相關地方的注釋,以便更好地理解:

class SampleViewController: UIViewController, UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource {

    let screenHeight = UIScreen.main.bounds.height
    let screenWidth = UIScreen.main.bounds.width

    let scrollView = UIScrollView()
    let tableView = UITableView()

    var colors:[UIColor] = [UIColor.red, UIColor.blue, UIColor.green, UIColor.yellow]
    let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat", "Dog", "Cat", "Kitten", "Lion", "Tiger", "Owl", "Skylark", "Snail", "Cub", "Zebra"]
    var frame: CGRect = CGRect(x:0, y:0, width:0, height:0)
    var pageControl : UIPageControl = UIPageControl(frame: CGRect(x:50,y: 300, width:200, height:50))
    let cellReuseIdentifier = "cell"

    override func viewDidLoad() {
        super.viewDidLoad()

        let frameOfScrollView : CGRect = CGRect(x:0, y:0, width:screenWidth, height:screenHeight)
        scrollView.frame = frameOfScrollView

        scrollView.delegate = self
        scrollView.isPagingEnabled = true

        tableView.delegate = self
        tableView.dataSource = self

        self.view.addSubview(scrollView)
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
        self.scrollView.contentSize = CGSize(width:self.scrollView.frame.size.width * 4,height: self.scrollView.frame.size.height)

        for index in 0..<4 {

            frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
            frame.size = self.scrollView.frame.size

            // THIS THING DOESN'T WORK
            tableView.frame = frame
            self.scrollView .addSubview(tableView)

            // THIS THING WORKS
            let subView = UILabel(frame: frame)
            subView.backgroundColor = colors[index]
            subView.text = "how are you?"
            self.scrollView .addSubview(subview)
        }
    }

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

        let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
        pageControl.currentPage = Int(pageNumber)
    }

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

    // create a cell for each table view row
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
            cell.textLabel?.text = self.animals[indexPath.row]
        return cell
    }
}

我究竟做錯了什么? 我錯過了任何可重塑tableview的東西嗎?

注意:我已經以編程方式創建了視圖,因為我對使用iOS的新手並沒有多大了解才能使用分頁來向scrollview添加分頁。 如果有人可以在自動布局方面為我的問題提供一些解決方案,那將是受歡迎的。 基本上,目的只是為了實現上面所附屏幕快照中顯示的內容。 如果適用,任何替代方法也將受到贊賞。

無需為UITableview聲明全局變量。 替換viewDidLoad()以實現您的要求。

override func viewDidLoad() {
        super.viewDidLoad()

        let frameOfScrollView : CGRect = CGRect(x:0, y:0, width:screenWidth, height:screenHeight)
        scrollView.frame = frameOfScrollView

        scrollView.delegate = self
        scrollView.isPagingEnabled = true

//        tableView.delegate = self
//        tableView.dataSource = self

        self.view.addSubview(scrollView)
//        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
        self.scrollView.contentSize = CGSize(width:self.scrollView.frame.size.width * 4,height: self.scrollView.frame.size.height)

        for index in 0..<4 {

            frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
            frame.size = self.scrollView.frame.size

            let tableView = UITableView()
            tableView.delegate = self
            tableView.dataSource = self
            tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
            tableView.frame = frame
            self.scrollView .addSubview(tableView)

////            // THIS THING DOESN'T WORK
//            tableView.frame = frame
//            self.scrollView .addSubview(tableView)

            // THIS THING WORKS
//            let subView = UILabel(frame: frame)
//            subView.backgroundColor = colors[index]
//            subView.text = "how are you?"
//            self.scrollView.addSubview(subView)
        }
    }

更換

let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!

let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell! 

去掉

let tableView = UITableView()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM