I have a UICollectionView that loads cells from a reusable cell containing a shortcut. The array provides content for this label. I can change the width of the label depending on the width of the content using the sizeToFit parameter. But I canβt make the cell a suitable label.
Here is the code
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. arrayOfStats = @[@"time:",@"2",@"items:",@"10",@"difficulty:",@"hard",@"category:",@"main"]; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section{ return [arrayOfStats count]; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ return CGSizeMake(??????????); } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ Cell *cell = (Cell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"qw" forIndexPath:indexPath]; cell.myLabel.text = [NSString stringWithFormat:@"%@",[arrayOfStats objectAtIndex:indexPath.item]]; // make label width depend on text width [cell.myLabel sizeToFit]; //get the width and height of the label (CGSize contains two parameters: width and height) CGSize labelSize = cell.myLbale.frame.size; NSLog(@"\n width = %f height = %f", labelSize.width,labelSize.height); return cell; }
ios objective-c swift uicollectionview uicollectionviewcell
pulp Apr 17 '14 at 13:49 2014-04-17 13:49
source share