我试图创建一个集合视图,显示单元格显示可变长度的字符串。CollectionView动态细胞高度迅速
即时通讯使用此功能设置单元布局:
func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize
{
var cellSize:CGSize = CGSizeMake(self.whyCollectionView.frame.width, 86)
return cellSize
}
什么,我想这样做是基于我cell.labelString.utf16Count
长度操纵cellSize.height
。 的基本逻辑将SA是
if((cell.labelString.text) > 70){
cellSize.height = x
}
else{
cellSize.height = y
}
但是,我不能设法找回我的电池标签字符串长度总是返回零。 (我认为这是没有载入...
为了更好的理解,这里是全码:
// WhyCell section
var whyData:NSMutableArray! = NSMutableArray()
var textLength:Int!
@IBOutlet weak var whyCollectionView: UICollectionView!
//Loading data
@IBAction func loadData() {
whyData.removeAllObjects()
var findWhyData:PFQuery = PFQuery(className: "PlacesWhy")
findWhyData.whereKey("placeName", equalTo: placeName)
findWhyData.findObjectsInBackgroundWithBlock({
(objects:[AnyObject]!,error:NSError!)->Void in
if (error == nil) {
for object in objects {
self.whyData.addObject(object)
}
let array:NSArray = self.whyData.reverseObjectEnumerator().allObjects
self.whyData = array.mutableCopy() as NSMutableArray
self.whyCollectionView.reloadData()
println("loadData completed. datacount is \(self.whyData.count)")
}
})
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.loadData()
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return whyData.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell:whyCollectionViewCell = whyCollectionView.dequeueReusableCellWithReuseIdentifier("whyCell", forIndexPath: indexPath) as whyCollectionViewCell
// Loading content from NSMutableArray to cell
let therew:PFObject = self.whyData.objectAtIndex(indexPath.row) as PFObject
cell.userWhy.text = therew.objectForKey("why") as String!
textLength = (therew.objectForKey("why") as String!).utf16Count
self.whyCollectionView.layoutSubviews()
// Displaying user information
var whatUser:PFQuery = PFUser.query()
whatUser.whereKey("objectId", equalTo: therew.objectForKey("reasonGivenBy").objectId)
whatUser.findObjectsInBackgroundWithBlock({
(objects: [AnyObject]!, error: NSError!)->Void in
if !(error != nil) {
if let user:PFUser = (objects as NSArray).lastObject as? PFUser {
cell.userName.text = user.username
// TODO Display avatar
}
}
})
return cell
}
func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize
{
var cellSize:CGSize = CGSizeMake(self.whyCollectionView.frame.width, 86)
return cellSize
}
真棒,我探索了几种方法来解决这个问题,但我没有看到在cellForItemAtIndexPath中这样做。感谢您的提示,我会让你知道! – SKYnine 2014-12-04 14:35:07
好吧,看来我可以使用你的方法调整高度,但现在我的问题变成了行之间的间距。我有一些我不需要的重要空间。我的故事板内所有设置都设置为零 – SKYnine 2014-12-05 02:17:31
高度[indexPath.row]是服务器数据对象。请告诉我。 – Malleswari 2018-03-10 08:21:36