【发布时间】:2014-07-13 04:04:03
【问题描述】:
我所做的是使用 label.sizeToFit() 将标签设置为调整大小,这很方便,但是我现在也需要设置单元格以正确调整其大小。
我使用 FlowLayoutDelegate 中的 sizeForIndexPath 方法将单元格大小设置为:
messages[indexPath.row].sizeWithAttributes(nil)
但是,这给了我非常薄/小的 CGSize。
import UIKit
let reuseIdentifier = "Cell"
class PhotoCollectionViewController: UICollectionViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
var messages = NSString[]()
override func viewDidLoad() {
super.viewDidLoad()
messages = ["Hello, How are you?", "Ldsfsd sdf dsfs s fs fs", "fsdfsdfsdfs fsdfsdfsdf sfs fs", "yoyo yoy oyo", "sdfd sfds fssfs"]
self.collectionView.registerClass(TextCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
self.collectionView.dataSource = self
self.collectionView.delegate = self
self.collectionView.collectionViewLayout = PhotoCollectionViewFlowLayout()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// #pragma mark UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView?) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView?, numberOfItemsInSection section: Int) -> Int {
//#warning Incomplete method implementation -- Return the number of items in the section
return messages.count
}
override func collectionView(collectionView: UICollectionView?, cellForItemAtIndexPath indexPath: NSIndexPath?) -> UICollectionViewCell? {
let cell = collectionView?.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as TextCollectionViewCell
cell.backgroundColor = UIColor.lightGrayColor()
cell.newLabel.text = messages[indexPath!.row]
cell.newLabel.sizeToFit()
return cell
}
func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize {
//println(messages[indexPath.row].sizeWithAttributes(nil))
return messages[indexPath.row].sizeWithAttributes(nil)
}
}
编辑:我已经让它工作了,有点(以未优化的方式):
func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize {
let cell = TextCollectionViewCell(frame: CGRect(x: 0, y: 8, width: 300, height: 100))
cell.newLabel.text = messages[indexPath.item]
cell.newLabel.sizeToFit()
println(cell.newLabel.intrinsicContentSize())
//var width = UIWindow.
return CGSizeMake(cell.newLabel.intrinsicContentSize().width+10, cell.newLabel.intrinsicContentSize().height+20)
//return cell.newLabel.intrinsicContentSize()
}
现在文本适合大多数情况: 当它的宽度比屏幕尺寸长时它不适合,因为显然新行不会自动开始。 它也显示在屏幕中间,不确定我可以在布局中的哪个位置更改它或者它是否依赖于其他东西。
【问题讨论】:
-
您是否设置了单元格的内容拥抱优先级?如果没有,请打开尺寸检查器并将其设置为 1000。
-
这没有造成任何差异,尽管这可能是因为我必须继承 UICollectionViewCell 并以编程方式添加标签,因为界面构建器在 Xcode 6 中的单元格中存在标签问题stackoverflow.com/questions/24492275/…
-
.sizeWithAttributes()应该使用实际标签文本的属性来调用。
标签: ios swift uicollectionview