ios - 在启用分页的情况下将 TableView 添加到 ScrollView 。

标签 ios swift uitableview uiscrollview scroll-paging

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

enter image description here

我已经完成创建启用分页的 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 进行重新分级的内容?

注意:我以编程方式创建了 View ,因为我对我应该使用哪些约束来向 ScrollView 添加分页没有太多了解,因为我是 iOS 的新手。如果有人可以在自动布局方面为我的问题提供一些解决方案,我们将不胜感激。 基本上,目的只是为了实现上面所附屏幕截图中显示的内容。如果适用,任何替代方法也将不胜感激。

最佳答案

无需为 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()

关于ios - 在启用分页的情况下将 TableView 添加到 ScrollView 。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48498176/

相关文章:

ios - 有没有办法在环境光变化时获得通知?

objective-c - Xcode/Cocoapods 为什么我不能从 Pod 实现 Swift 协议(protocol)?

ios - 为什么我的 inputAccessoryView 在向后导航时消失了?

ios - tableView 更新期间对数据源的线程安全访问

ios - 是否有可能在NSMutableArray中混淆addObject :?

ios - 何时使用谓词通过 prepare 方法传递 CoreData 对象

ios - 为什么我已经设置了一个原型(prototype)单元但什么也没有出现?

ios - UITableView 一直自动滚动到顶部

iphone - 如何在 UIScrollView 中使用 UITableView 并接收单元格点击?

ios - 将大小类添加到现有的 Xcode 项目