Im使用此功能设置单元格布局:
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.
基本逻辑就是这样
if((cell.labelString.text) > 70){ cellSize.height = x}else{ cellSize.height = y}
但是,我无法检索我的单元格标签长度,总是返回nil. (我觉得没有加载…
为了更好的理解,这里是完整的代码:
// 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,86) return cellSize }您可以在cellForItemAtIndexPath函数中动态设置单元格的框架,因此如果忽略sizeforItemAtIndexPath函数,您可以根据标签自定义高度.通过自定义大小,您可能需要查看集合视图布局流程,但希望这可以指向正确的方向.它可能看起来像这样:
class CollectionVIEwController: UICollectionVIEwController,UICollectionVIEwDelegate,UICollectionVIEwDataSource,UICollectionVIEwDelegateFlowLayout { var array = ["a","as","asd","asdf","asdfg","asdfgh","asdfghjk","asdfghjklas","asdfghjkl","asdghjklkjhgfdsa"] 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] overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() } overrIDe func numberOfSectionsInCollectionVIEw(collectionVIEw: UICollectionVIEw) -> Int { return 1 } overrIDe func collectionVIEw(collectionVIEw: UICollectionVIEw,numberOfItemsInSection section: Int) -> Int { return array.count } overrIDe func collectionVIEw(collectionVIEw: UICollectionVIEw,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionVIEwCell { let cell = collectionVIEw.dequeueReusableCellWithReuseIDentifIEr("CellID",forIndexPath: indexPath) as Cell cell.textLabel.text = array[indexPath.row] cell.textLabel.sizetoFit() // Customize cell height cell.frame = CGRectMake(cell.frame.origin.x,cell.frame.origin.y,cell.frame.size.wIDth,heights[indexPath.row]) return cell } func collectionVIEw(collectionVIEw: UICollectionVIEw,layout collectionVIEwLayout: UICollectionVIEwLayout,sizeforItemAtIndexPath indexPath: NSIndexPath) -> CGSize { return CGSizeMake(64,64) }}
这给了动态高度
总结以上是内存溢出为你收集整理的布局 – CollectionView动态单元格高度swift全部内容,希望文章能够帮你解决布局 – CollectionView动态单元格高度swift所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)