UICollectionViewCell使用原型大小 [英] UICollectionViewCell using prototype sizes
问题描述
我有一个带有三个不同原型单元的UICollectionView,每个原型单元通过Storyboard设置不同的高度。在运行时,Collection View使用自己的单元格大小,忽略我的Storyboard。
I have an UICollectionView with three different prototype cells, each of which with different heights set via Storyboard. During runtime, the Collection View uses its own cell size, ignoring my Storyboard ones.
我目前正在使用collectionView:layout:sizeForItemAtIndexPath:带有几个条件来设置每个CGSize直接。
I am currently using collectionView:layout:sizeForItemAtIndexPath: with a couple conditionals to set each CGSize straight.
有没有更好的方法来设置单元格大小?我似乎无法检索每个单元格的Storyboard大小,CGSizeMake似乎太硬编码而且不太灵活。
Is there a better way to set the cell sizes? I don't seem to be able to retrieve the Storyboard size each cell has, and CGSizeMake seems too hardcoded and not really flexible.
推荐答案
似乎目前没有简单的方法:
It seems that there's currently no easy way to:
- 从故事板中获取UICollectionViewCell原型单元大小运行时。
- 只需在一个地方管理原型单元的大小(而不必在Storyboard单元原型中输入它们并实现
sizeForItemAtIndexPath
)。
- Fetch UICollectionViewCell prototype cell sizes runtime from Storyboard(s).
- Manage sizes of prototype cells just in one place (rather than having to enter them in the Storyboard cell prototype and implement
sizeForItemAtIndexPath
).
建议的方法这里(对于UITableViews)不起作用,因为在 sizeForItemAtIndexPath
中使用 dequeueReusableCellWithReuseIdentifier
将导致无限循环。
A method proposed here (for UITableViews) does not work, because using dequeueReusableCellWithReuseIdentifier
in sizeForItemAtIndexPath
will cause an indefinite loop.
但是,我已设法通过以下方式执行此操作:
-
添加一个唯一的(跨越所有
UICollectionViewCell
在每个故事板中)在所有故事板中的每个UICollectionView
原型单元格中重复使用标识符。
Add a unique (across all
UICollectionViewCell
s in every storyboard) reuse identifier into each of yourUICollectionView
prototype cells in all Storyboards.
使用提取的脚本 UICollectionViewCell <为您的项目添加
运行脚本
构建阶段/ code>所有故事板中的帧大小。
Add a Run script
Build Phase to your project with the script that pulls out UICollectionViewCell
frame sizes from all Storyboards.
output=${PROJECT_DIR}/StoryboardPrototypeCellSizes.h
printf "@{" > $output
for storyboard in $(find ${PROJECT_DIR} -name "*.storyboard")
do
echo "Scanning storyboard $storyboard..."
delimiter=
for line in $(xpath $storyboard "//collectionViewCell/@reuseIdentifier[string-length()>0] | //collectionViewCell/rect" 2>&-)
do
case $line in
reuseIdentifier*)
reuseIdentifier=$(sed 's/[^"]*"\([^"]*\)".*/\1/' <<< $line)
;;
width*)
if [ -n "$reuseIdentifier" ]; then
width=$(sed 's/[^"]*"\([^"]*\)".*/\1/' <<< $line)
fi
;;
height*)
if [ -n "$reuseIdentifier" ]; then
height=$(sed 's/[^"]*"\([^"]*\)".*/\1/' <<< $line)
fi
;;
esac
if [ -n "$reuseIdentifier" ] && [ -n "$width" ] && [ -n "$height" ]; then
printf "$delimiter@\"$reuseIdentifier\" : [NSValue valueWithCGSize:CGSizeMake($width, $height)]" >> $output
unset reuseIdentifier
unset width
unset height
delimiter=,\\n
fi
done
done
printf "};\n" >> $output
这将创建一个名为 StoryboardPrototypeCellSizes.h的头文件
以下示例内容:
This creates a header file called StoryboardPrototypeCellSizes.h
with a following example content:
@{@"TodayCell" : [NSValue valueWithCGSize:CGSizeMake(320, 80)],
@"SpecialDayCell" : [NSValue valueWithCGSize:CGSizeMake(320, 42)],
@"NameDayCell" : [NSValue valueWithCGSize:CGSizeMake(320, 30)]};
添加帮助方法以返回 UICollectionViewCell
在控制 UICollectionView
的视图控制器中重用标识符:
Add a helper method to return the UICollectionViewCell
reuse identifier in the view controller controlling your UICollectionView
:
- (NSString *)cellReuseIdentifierAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.item) {
case 0: return @"TodayCell";
case 1: return @"SpecialDayCell";
case 2: return @"NameDayCell";
}
return nil;
}
请务必在<$ c $中使用相同的重用标识符c> cellForItemAtIndexPath :
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell =
[collectionView dequeueReusableCellWithReuseIdentifier:
[self cellReuseIdentifierAtIndexPath:indexPath]
forIndexPath:indexPath];
...
最后实现 sizeForItemAtIndexPath
:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *storyboardPrototypeCellSizes =
#import "StoryboardPrototypeCellSizes.h"
return [(NSValue *)storyboardPrototypeCellSizes[
[self cellReuseIdentifierAtIndexPath:indexPath]
] CGSizeValue];
}
此解决方案可让您在故事板中仅定义 UICollectionViewCell
原型单元大小一次,并且在运行时也不执行任何非App-Store兼容。
This solution allows you to define UICollectionViewCell
prototype cell sizes only once in the Storyboard(s) and also doesn't do any non-App-Store-compliant at runtime.
****编辑:****您还可以通过添加具有相同内容的另一个脚本并将collectionViewCell替换为collectionReusableView并将头文件重命名为,来获取UICollectionReusableView大小,例如,StoryboardReusableViewSizes.h
******** You can also fetch UICollectionReusableView sizes by adding another script with the same content and replacing "collectionViewCell" with "collectionReusableView", and renaming the header file to, for example, StoryboardReusableViewSizes.h
这篇关于UICollectionViewCell使用原型大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!