将tableview添加到scrollview并启用分页。

时间:2018-01-29 09:37:13

标签: ios swift uitableview uiscrollview scroll-paging

我正在创建一个应用程序,其中一个屏幕需要有一个带分页的UIScrollView。在scrollview的每个页面中,它需要有一个UITableView,如下所示:

enter image description here

我已经完成了创建一个启用分页的滚动视图(以编程方式),但我面临的问题是: 如果我创建一个简单的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的内容?

注意:我已经以编程方式创建了视图,因为我没有太多了解我应该使用哪些约束来向scrollview添加分页,因为我是iOS的新手。如果有人能够在汽车布局方面为我的问题提供一些解决方案,那将是受欢迎的。 基本上,目的只是为了实现上面附带的屏幕截图中显示的内容。如果适用,任何替代方法也将受到赞赏。

1 个答案:

答案 0 :(得分:3)

无需为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()
相关问题