установить динамическую ширину и высоту ячейки представления коллекции

На самом деле я хочу установить кнопку динамически и заголовок кнопки появляется Динамический Я попробовал это и получил ширину динамического содержимого, но я не устанавливаю ширину и высоту ячейки.

- (void)viewDidLoad {
    [super viewDidLoad];

    ContentArray = [[NSMutableArray alloc]init];

    self.collectionview.delegate = self;
    self.collectionview.dataSource =self;
    self.collectionview.backgroundColor = [UIColor clearColor];


    [ContentArray addObject:@"Planning"];
    [ContentArray addObject:@"Resources"];
    [ContentArray addObject:@"Human Resources"];
    [ContentArray addObject:@"Agriculture"];
    [ContentArray addObject:@"Resources management"];


}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        return ContentArray.count;
    }


    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {

        CollectionViewCell * Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell"forIndexPath:indexPath];

        [Cell.button setTitle:[ContentArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];

        return Cell;
    }


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {


        CGRect rect = [[ContentArray objectAtIndex:indexPath.row ] boundingRectWithSize:CGSizeMake(HUGE_VALF, 50) options:NSStringDrawingUsesFontLeading attributes:nil context:nil] ;


        return CGSizeMake(rect.size.width, 50);
    }

и вывод такой

Это идеально, я думаю, если я сделаю ячейку динамической ширины и высоты, поэтому, пожалуйста, проведите меня через это.

Ответы на вопрос(4)

Ваш ответ на вопрос