Dynamic resizing of iOS UICollectionView cell

I. Introduction

UICollectionView should be the most frequently used control after UITableView among the list controls we use. In many scenarios, it is necessary to dynamically calculate and modify the width and height of UICollectionViewCell. Is this method provided in the proxy method?

2. Code

Implement this method and calculate the width and height you need in the method and return it.

- (CGSize)collectionView: (UICollectionView *)collectionView layout: (UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath: (NSIndexPath *)indexPath {
    
    
	NSDictionary *dict = @{
    
    NSFontAttributeName: [UIFont systemFontOfSize:18]};
	NSString *titleStr = @"测试文本";
	CGSize detailSize = [titleStr sizeWithAttributes:dict];
	CGSize size = CGSizeMake(detailSize.width + 35, 50);//每个cell的宽度自适应
	return size;
}

It is a very easy to use method, but I have a question, that is, this method is not included in the official proxy method. I am not sure yet. I will update it later.

Guess you like

Origin blog.csdn.net/weixin_44758107/article/details/128208852