【问题标题】:uiCollectionViews in a uitableViewCells with swiftuiCollectionViews 在 uitableViewCells 中使用 swift
【发布时间】:2014-10-23 01:36:21
【问题描述】:

我已经在旧版 xcode 的 tableview 单元中看到了许多 uicollectionview 的示例,但是在查找/弄清楚如何使用 swift 进行开发时遇到了麻烦。我一直在玩这个源代码...http://www.brianjcoleman.com/tutorial-collection-view-using-swift/,但我想知道如何使用 Swift 在 tableViewCells 中引入或构建 UICollectionViews...?

提前感谢您提供有关如何使用 Swift 和 Storyboards 执行此操作的任何指导或资源?

//  ViewController.swift
//  UICollectionView
import UIKit

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    @IBOutlet var collectionView: UICollectionView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: 90, height: 90)
        collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        collectionView!.dataSource = self
        collectionView!.delegate = self
        collectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
        collectionView!.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(collectionView!)
    }

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 20
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as CollectionViewCell
        cell.backgroundColor = UIColor.blackColor()
        cell.textLabel?.text = "\(indexPath.section):\(indexPath.row)"
        cell.imageView?.image = UIImage(named: "circle")
        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}



//  CollectionViewCell.swift
//  UICollectionView
import UIKit

class CollectionViewCell: UICollectionViewCell {

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    let textLabel: UILabel!
    let imageView: UIImageView!

    override init(frame: CGRect) {
        super.init(frame: frame)

        imageView = UIImageView(frame: CGRect(x: 0, y: 16, width: frame.size.width, height: frame.size.height*2/3))
        imageView.contentMode = UIViewContentMode.ScaleAspectFit
        contentView.addSubview(imageView)

        let textFrame = CGRect(x: 0, y: 32, width: frame.size.width, height: frame.size.height/3)
        textLabel = UILabel(frame: textFrame)
        textLabel.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
        textLabel.textAlignment = .Center
        contentView.addSubview(textLabel)
    }
}

【问题讨论】:

    标签: uitableview swift uicollectionview xcode6


    【解决方案1】:

    你在这里;)

    class ViewController: UITableViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    
    let data: Array<Int>= [1,2,3,4,5,6,7,8,9,10]
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 20
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as UICollectionViewCell        
        return cell
    }
    }
    

    【讨论】:

      【解决方案2】:

      这是一个 Swift 2.0 在 collectionViewCell 中的 collectionView 示例,但同样适用于 tableView。

      https://github.com/irfanlone/Collection-View-in-a-collection-view-cell

      在这种情况下,使用集合视图肯定比表格视图有一些优势。其他有用的链接:

      教程:https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/

      源代码:https://github.com/ashfurrow/Collection-View-in-a-Table-View-Cell

      希望有帮助!!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多