我试图调整单元格高度调整大小以适应UILabel文本,但它不工作..
var mySize = CGFloat() func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! cellView cell.myLabel.text = self.items[indexPath.item] cell.myLabel.bounds.size.height = self.mySize cell.backgroundColor = UIColor.yellowColor() return cell } func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { // handle tap events print("You selected cell #\(indexPath.item)!") } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { func heightForLabel(text:String, font:UIFont, width:CGFloat) -> CGFloat { let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, CGFloat.max)) label.numberOfLines = 0 label.lineBreakMode = NSLineBreakMode.ByWordWrapping label.font = font label.text = items[indexPath.row] label.sizeToFit() return label.frame.height } let font = UIFont(name: "Helvetica Neue", size: 30) let detailHeight = heightForLabel(items[indexPath.row], font: font!, width: UIScreen.mainScreen().bounds.size.width) self.mySize = detailHeight return CGSizeMake(UIScreen.mainScreen().bounds.size.width, 358 + detailHeight) }
有什么建议在这做什么?我应该用另一种方式吗?请,我需要帮助..问题是UILabel文本设置在cellForItemAtIndexPath
,并且items
是一个字符串数组.
这是我的项目文件,如果有人注意看看它:http: //www.filedropper.com/test_37