I have a collection view with a custom flow layout, and many different cells of different height. The width of the collection view changes on device rotation, so the width of cells must change accordingly. For this to work, I implemented “sizeForItemAtIndex” method, and return different sizes depending on current interface orientation.
Most of the cells do not change their height, however, there is one cell that I want to expand and collapse whenever the user taps on it. You can assume that the cell only has one UILabel with one or more lines of text. When the cell appear for the first time the number of lines is set to 1, and when user taps on the cell the number of lines is set to 0, and here the cell should use the intrinsic size of the label to change it’s height automatically. How can I achieve this effect?
Here is an example of what it should look like:
按着这些次序:
1)。创建一个名为的布尔变量isExpanded
管理展开/折叠
2.) 将目标和操作添加到显示更多按钮
[yourCellName.btnShowMore addTarget:self action:@selector(ShowMoreButtonClicked) forControlEvents:UIControlEventTouchUpInside];
3.) 在用于管理高度的 sizeForItemAtIndexPath 中,添加:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (isExpanded && indexPath.row == 0) {
return CGSizeMake(CGRectGetWidth(self.view.frame), calculated height for the expanded cell);
}
return CGSizeMake(CGRectGetWidth(self.view.frame), default height);
}
4.) 然后在 ShowMoreButtonClicked 方法中
- (void)ShowMoreButtonClicked{
if (isExpanded) {
isExpanded = FALSE;
[collection_viewCus reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]]];
}
else {
isExpanded = TRUE;
[collection_viewCus reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]]];
}}
5.) 将此行添加到您的 cellForItemAtIndexPath 中
[yourCellName layoutIfNeeded];
6.) 构建并运行
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)