使用UIPageControl显示多个UICollectionViews

时间:2019-05-09 08:06:31

标签: ios swift uicollectionview uibutton uipagecontrol

-我是iOS开发的新手-

我目前正在为一家餐厅编写应用程序。 该应用程序的功能是,员工可以在将设备交给他们之前选择顾客坐在哪张桌子上。

我目前正在尝试通过使用“集合视图”将每个表列为一个按钮来实现此目的。

表分为两组:内部和外部。

我的目标是使用页面控件将“内部”表和“外部”表分开。

这是我到目前为止所拥有的:

Page 1: #1 to #24 resembles the Inside Tables, whilst the rest resemble the Outside Tables

Page 2: Empty

第1类:CollectionViewCell

class CollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var tableButton: UIButton!
}

第2类:TableNumSelection

class TableNumSelection: UIViewController, UIScrollViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate {

@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var collectionView: UICollectionView!

@IBOutlet weak var typeLabel: UILabel!

let types = ["Inside", "Outside"]

let tables = [["#1", "#2", "#3", "#4", "#5", "#6", "#7", "#8", "#9", "#10", "#11", "#12", "#13", "#14", "#15", "#16", "#17", "#18", "#19", "#20", "#21", "#22", "#23", "#24"], ["#501", "#502", "#503", "#504", "#505", "#506", "#507", "#508", "#509", "#510", "#511", "#512", "#513", "#514", "#515", "#516", "#517", "#518", "#519", "#520", "#521", "#522", "#523", "#524", "#525", "#526", "#527", "#528", "#529", "#530", "#531"]]

var frame = CGRect(x:0,y:0,width:0,height:0)

override func viewDidLoad() {
    super.viewDidLoad()
    pageControl.numberOfPages = 2

    for i in 0..<tables.count {
        frame.origin.x = scrollView.frame.size.width * CGFloat(i)
        frame.size = scrollView.frame.size

        typeLabel.text = types[i]

        let numberOfItems = collectionView(collectionView, numberOfItemsInSection: i)
        print(numberOfItems)
    }

    scrollView.contentSize = CGSize(width: (scrollView.frame.size.width * CGFloat(tables.count)), height: scrollView.frame.size.height)
    scrollView.delegate = self
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    let pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width
    pageControl.currentPage = Int(pageNumber)
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 2
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if section == 0 {
        return tables[0].count
    }
    else {
        return tables[1].count
    }
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell

    print(indexPath)
    let section = indexPath.section

    if section == 0 {
        cell.tableButton.setTitle(tables[0][indexPath.item], for: UIControl.State.normal)
        cell.tableButton.setTitleColor(UIColor.white, for: UIControl.State.normal)
        return cell
    } else {
        cell.tableButton.setTitle(tables[1][indexPath.item], for: UIControl.State.normal)
        cell.tableButton.setTitleColor(UIColor.white, for: UIControl.State.normal)
        return cell
    }
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print(indexPath.item)
}

滑动和页面切换功能没有问题,因为它可以正常工作,但是:

我尝试将表分成两部分,尽管我不知道如何在单独的页面上显示每个部分(例如,第1页的第0部分和第2页的第1部分)。

我需要创建两个集合视图吗?还是我尝试而不是成功尝试按部分简单地分开?

The Scene hierarchy

任何指导将不胜感激。

0 个答案:

没有答案