美文网首页
UICollectionView

UICollectionView

作者: 79d12e22ec53 | 来源:发表于2019-04-28 19:04 被阅读0次

    UICollectionView

    import UIKit
    
    class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let screenRect = UIScreen.main.bounds
            let rect = CGRect(x: 0, y: 20, width: screenRect.size.width, height: screenRect.size.height / 3)
    
            let layout = UICollectionViewFlowLayout()
            layout.scrollDirection = .horizontal
            layout.itemSize = CGSize(width: 200, height: 200)
            layout.minimumLineSpacing = 30
            layout.minimumInteritemSpacing = 10
            let collectionView = UICollectionView(frame: rect, collectionViewLayout: layout)
            collectionView.backgroundColor = UIColor.white
            collectionView.register(NSClassFromString("UICollectionViewCell"), forCellWithReuseIdentifier: "reusedCell")
            collectionView.delegate = self
            collectionView.dataSource = self
            self.view.addSubview(collectionView)
        }
    
        func numberOfSections(in collectionView: UICollectionView) -> Int {
            return 1
        }
    
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 10
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "reusedCell", for: indexPath)
            cell.backgroundColor = UIColor.blue
            return cell
        }
    
    }
    

    相关文章

      网友评论

          本文标题:UICollectionView

          本文链接:https://www.haomeiwen.com/subject/srhngqtx.html