布局 – CollectionView动态单元格高度swift

前端之家收集整理的这篇文章主要介绍了布局 – CollectionView动态单元格高度swift前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图创建一个集合视图,其中单元格显示具有可变长度的字符串.

Im使用此功能设置单元格布局:

  1. func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize
  2. {
  3. var cellSize:CGSize = CGSizeMake(self.whyCollectionView.frame.width,86)
  4. return cellSize
  5. }

我想做的是根据我的cell.labelString.utf16Count长度操纵cellSize.height.
基本逻辑就是这样

  1. if((cell.labelString.text) > 70){
  2. cellSize.height = x
  3. }
  4. else{
  5. cellSize.height = y
  6. }

但是,我无法检索我的单元格标签长度,总是返回nil. (我觉得没有加载…

为了更好的理解,这里是完整的代码

  1. // WhyCell section
  2. var whyData:NSMutableArray! = NSMutableArray()
  3. var textLength:Int!
  4. @IBOutlet weak var whyCollectionView: UICollectionView!
  5.  
  6. //Loading data
  7. @IBAction func loadData() {
  8. whyData.removeAllObjects()
  9.  
  10. var findWhyData:PFQuery = PFQuery(className: "PlacesWhy")
  11. findWhyData.whereKey("placeName",equalTo: placeName)
  12.  
  13. findWhyData.findObjectsInBackgroundWithBlock({
  14. (objects:[AnyObject]!,error:NSError!)->Void in
  15.  
  16. if (error == nil) {
  17. for object in objects {
  18. self.whyData.addObject(object)
  19. }
  20.  
  21. let array:NSArray = self.whyData.reverSEObjectEnumerator().allObjects
  22. self.whyData = array.mutableCopy() as NSMutableArray
  23.  
  24. self.whyCollectionView.reloadData()
  25. println("loadData completed. datacount is \(self.whyData.count)")
  26. }
  27. })
  28. }
  29.  
  30. override func viewDidLoad() {
  31. super.viewDidLoad()
  32.  
  33. // Do any additional setup after loading the view.
  34. self.loadData()
  35.  
  36.  
  37. }
  38.  
  39. func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
  40. return 1
  41. }
  42.  
  43. func collectionView(collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
  44. return whyData.count
  45. }
  46.  
  47. func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
  48. let cell:whyCollectionViewCell = whyCollectionView.dequeueReusableCellWithReuseIdentifier("whyCell",forIndexPath: indexPath) as whyCollectionViewCell
  49.  
  50. // Loading content from NSMutableArray to cell
  51. let therew:PFObject = self.whyData.objectAtIndex(indexPath.row) as PFObject
  52. cell.userWhy.text = therew.objectForKey("why") as String!
  53. textLength = (therew.objectForKey("why") as String!).utf16Count
  54. self.whyCollectionView.layoutSubviews()
  55.  
  56. // Displaying user information
  57. var whatUser:PFQuery = PFUser.query()
  58. whatUser.whereKey("objectId",equalTo: therew.objectForKey("reasonGivenBy").objectId)
  59.  
  60. whatUser.findObjectsInBackgroundWithBlock({
  61. (objects: [AnyObject]!,error: NSError!)->Void in
  62.  
  63. if !(error != nil) {
  64. if let user:PFUser = (objects as NSArray).lastObject as? PFUser {
  65. cell.userName.text = user.username
  66. // TODO Display avatar
  67. }
  68.  
  69. }
  70. })
  71.  
  72. return cell
  73. }
  74.  
  75. func collectionView(collectionView : UICollectionView,86)
  76. return cellSize
  77. }
您可以在cellForItemAtIndexPath函数中动态设置单元格的框架,因此如果忽略sizeForItemAtIndexPath函数,您可以根据标签自定义高度.通过自定义大小,您可能需要查看集合视图布局流程,但希望这可以指向正确的方向.它可能看起来像这样:
  1. class CollectionViewController: UICollectionViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
  2.  
  3. var array = ["a","as","asd","asdf","asdfg","asdfgh","asdfghjk","asdfghjklas","asdfghjkl","asdghjklkjhgfdsa"]
  4. var heights = [10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0,110.0] as [CGFloat]
  5.  
  6. override func viewDidLoad() {
  7. super.viewDidLoad()
  8. }
  9.  
  10.  
  11. override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
  12. return 1
  13. }
  14.  
  15. override func collectionView(collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
  16. return array.count
  17. }
  18.  
  19. override func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
  20.  
  21. let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CellID",forIndexPath: indexPath) as Cell
  22. cell.textLabel.text = array[indexPath.row]
  23. cell.textLabel.sizeToFit()
  24.  
  25. // Customize cell height
  26. cell.frame = CGRectMake(cell.frame.origin.x,cell.frame.origin.y,cell.frame.size.width,heights[indexPath.row])
  27. return cell
  28. }
  29.  
  30. func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
  31. return CGSizeMake(64,64)
  32. }
  33. }

这给了动态高度

猜你在找的Swift相关文章