2016-06-01 6 views
0

カスタムUITableViewCellを構築する必要がありますが、コンテンツの処理方法についてはわかりません。指定されたデザインは、次のようになります。UITableViewCellのUICollectionView?

私はラベルやボーダー、すべてを整理しているが、コンテンツが動的であるという事実を考慮すると、私はそれを整理するために使用するのか分かりません内部。

私はFlowLayoutについて読んだことがありますが、使用方法やUICollectionViewについてもわかりませんが、コレクションビューで提供できるものはセル内の水平スクロールです。私のニーズに合ってください。

これを達成する方法についていくつかお勧めしますか?

答えて

2

ここでは、ダイナミックセルサイジングを使用するUICollectionViewの非常に基本的な実装を示します。あなたはsizeForItemAtIndexPath方法で、必要に応じて書式設定をクリーンアップする必要があります

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

希望すると便利です。

+0

これについてもっと考えてみると、タグの順序が重要でない場合は、レイアウトを最適化するための並べ替え関数を書くよりも(おそらく完全な正当化を行う) – DJohnson

関連する問題