【问题标题】:How to make nine cells side by side in collection view [Xcode] [Swift 3]如何在集合视图中并排制作九个单元格 [Xcode] [Swift 3]
【发布时间】:2017-04-29 08:18:39
【问题描述】:

我以前从未使用过集合视图,虽然有几个教程我没有找到我的问题的答案。 我在 ViewController 中使用集合视图,而不是在 CollectionViewController 中。

我想并排制作特殊数量的单元格(在我的项目中:九个)。 但我想在单元格周围设置边框(最终它们可能有不同的厚度,但这并不重要)。

谁能帮帮我?

谢谢。

【问题讨论】:

    标签: swift xcode uicollectionview uicollectionviewcell


    【解决方案1】:

    这将帮助您开始水平滚动Creating a horizontal scrolling collectionview in Swift

    要设置单元格,您需要实现 UICollectionViewDelegate 和方法(在这里您可以使用数据设置单元格):

    func dequeueReusableCell(withReuseIdentifier identifier: String, for indexPath: IndexPath) -> UICollectionViewCell
    

    只是不要忘记为单元格注册 nib 或类,否则它将不起作用(如果您将单元格添加到情节提要的集合视图中,则无需在代码中执行此操作)

    【讨论】:

    • 我对此并不陌生。我曾多次使用 tableViews,我可以对其进行编程。我已经用工作集合视图UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UITextFieldDelegate 制作了一个很好的工作程序,但问题是:我如何格式化它,九个单元格并排。​​
    • 好的,然后设置collectionViewFlowLayout.scrollDirection = .horizontal
    【解决方案2】:

    如果这对你有帮助,请告诉我。

    我不使用情节提要。

    AppDelegate.swift

        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
    
            window = UIWindow(frame: UIScreen.main.bounds)
            window?.makeKeyAndVisible()
    
            let layout = UICollectionViewFlowLayout()
            layout.scrollDirection = .horizontal
    
            let nineCellsController = NineCellsController(collectionViewLayout: layout)
    
            window?.rootViewController = nineCellsController
            return true
        }
    

    NineCellsController.swift

    import UIKit
    
    class NineCellsController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
    
        let cellIdentifier = "cellIdentifier"
        let numberOfCells = 9
    
        override func viewDidLoad() {
            super.viewDidLoad()
            collectionView?.backgroundColor = .white
    
            setupCollectionView()    
        }
    
        func setupCollectionView() {
            collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellIdentifier)
        }
    
        override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return numberOfCells
        }
    
        override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath)
            cell.backgroundColor = randomColor()
            return cell
        }
    
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            return CGSize(width: view.frame.size.width, height: view.frame.size.height)
        }
    
        fileprivate func randomColor() -> UIColor {
            let red = CGFloat(drand48())
            let green = CGFloat(drand48())
            let blue = CGFloat(drand48())
            return UIColor(red: red, green: green, blue: blue, alpha: 1)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-19
      • 2018-07-23
      • 2018-06-02
      • 2017-10-30
      • 2017-11-14
      • 2023-03-19
      • 2020-04-16
      • 1970-01-01
      相关资源
      最近更新 更多