[英]UICollectionView in UITableViewCell?
這是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
}
}
您必須根據需要清理sizeForItemAtIndexPath方法中的格式。
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.