2016-06-01 20 views
0

我不得不建立一个自定义的UITableViewCell,但我不确定如何处理这些内容。指定的设计是这样的:UITollectionView in UITableViewCell?

我已经整理出来的标签,边界和一切,但考虑到事实的内容是动态的,我不知道用什么来组织它内。

我已阅读关于FlowLayout,但我不知道如何使用它,也关于UICollectionView,但集合视图可以提供的所有内容是单元格内的水平滚动,一个功能不符合我的需求。

你能给我一些关于如何实现这一目标的建议吗?

回答

2

这是UICollectionView的一个非常基本的实现,它使用动态单元大小调整。

class CollectionViewController: UICollectionViewController { 

    @IBOutlet var myCollectionView: UICollectionView! 

    let tags = [ 
    "Web Design", 
    "ADWE", 
    "Busisness functions", 
    "Comics", 
    "Web", 
    "News in the Middle East", 
    "Albanian", 
    "Possession of machetes", 
    "Nuclear physics", 
    "Cookery", 
    "Cross stitiching" 
    ] 

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

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return tags.count 

    } 

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("dynamicCell", forIndexPath: indexPath) 
    cell.layer.borderColor = UIColor.grayColor().CGColor 
    cell.layer.borderWidth = 1.0 
    cell.layer.cornerRadius = cell.frame.height/2.0 
    let tagLabel = cell.viewWithTag(100) as? UILabel 
    tagLabel?.text = tags[indexPath.row] 
    return cell 
    } 

} 

extension CollectionViewController: UICollectionViewDelegateFlowLayout { 
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 

    let tagLabel = UILabel() 
    tagLabel.text = tags[indexPath.row] 
    let size = tagLabel.intrinsicContentSize() 
    let cellSize = CGSizeMake(size.width + 40, size.height + 20) //added 40 to width to add space around label. 

    return cellSize 

    } 
} 

enter image description here

你必须根据需要在sizeForItemAtIndexPath方法来清理格式。

希望这会有所帮助!

+0

想更多地了解这一点,如果标签的顺序并不重要,那么我会写一个排序函数来优化布局(并且可能会做充分的理由)。 – DJohnson