我在我的一个应用程序 UICollectionView 中使用自定义单元格,但使用默认流程。 目前,此 View 连续显示 3 个单元格(所有单元格的大小都相同)。单元格从左到右填充行,但我需要从右到左。
这可能吗?我需要创建自定义流程布局吗?如果是这样,谁能举个简单的例子?
我们将不胜感激。
最佳答案
假设您只需要三个单元格,并为此 Collection View 使用标题 View ,您将需要执行以下步骤:
覆盖 UICollectionViewFlowLayout
@interface GSRightToLeftCollectionViewFlowLayout : UICollectionViewFlowLayout
(GS 代表 Groboot SmarTech :))
.m 应该是这样的:
#import "GSRightToLeftCollectionViewFlowLayout.h"
typedef enum {
// enum for comfortibility.
CellXPositionLeft = 1,
CellXpositionRight = 2,
CellXpositionCenter = 3,
CellXPositionNone = 4
} CellXPosition;
@interface GSRightToLeftCollectionViewFlowLayout ()
@property (nonatomic) BOOL cellFlag;
@property (nonatomic) float cellLeftX;
@property (nonatomic) float cellMiddleX;
@property (nonatomic) float cellRightX;
@end
@implementation GSRightToLeftCollectionViewFlowLayout
// when ever the bounds change, call layoutAttributesForElementsInRect:
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *allItems = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
for (UICollectionViewLayoutAttributes *attribute in allItems) {
// when we get an item, calculate it's English writing position,
// so that we can convert it to the Hebrew - Arabic position.
if (!self.cellFlag) {
[self calculateLocationsForCellsWithAttribute:attribute];
self.cellFlag = YES;
}
// if it's a header, do not change it's place.
if (attribute.representedElementKind == UICollectionElementKindSectionHeader) {
continue;
}
// check where the item should be placed.
CellXPosition position = [self attributeForPosition:attribute];
switch (position) {
case CellXPositionLeft:
attribute.frame = CGRectMake(self.cellLeftX, attribute.frame.origin.y, attribute.frame.size.width, attribute.frame.size.height);
break;
case CellXpositionRight:
attribute.frame = CGRectMake(self.cellRightX, attribute.frame.origin.y, attribute.frame.size.width, attribute.frame.size.height);
break;
case CellXpositionCenter:
attribute.frame = CGRectMake(self.cellMiddleX, attribute.frame.origin.y, attribute.frame.size.width, attribute.frame.size.height);
break;
case CellXPositionNone:
NSLog(@"warning");
break;
}
}
return allItems;
}
- (CellXPosition)attributeForPosition:(UICollectionViewLayoutAttributes *)attribute
{
CellXPosition cellXposition = CellXPositionNone;
// we will return an opposite answer
if (attribute.indexPath.row % 3 == 0) {
// if it's in the left side, move it to the right
cellXposition = CellXpositionRight;
} else if (attribute.indexPath.row % 3 == 1) {
cellXposition = CellXpositionCenter;
} else if (attribute.indexPath.row % 3) {
// if it's in the right side, move it to the left
cellXposition = CellXPositionLeft;
}
return cellXposition;
}
- (void)calculateLocationsForCellsWithAttribute:(UICollectionViewLayoutAttributes *)attribute
{
float cellWidth = self.itemSize.width;
float minimumX = self.sectionInset.left;
float maximumX = self.sectionInset.right;
float displayWidth = self.collectionView.contentSize.width - minimumX - maximumX;
// on iOS6, displayWidth will be 0 (don't know why), so insert an if (displayWidth == 0) and set manually the size.
self.cellLeftX = minimumX;
float space = (displayWidth - cellWidth * 3) / 2;
self.cellMiddleX = self.cellRightX + cellWidth + space;
self.cellRightX = self.cellMiddleX + cellWidth + space;
}
@end
在显示 CollectionView 的类中,您需要这样做:
如果您使用 Storyboard: 1. 将 collectionView 布局更改为自定义(在属性检查器中) 2. 将其类设置为 GSRightToLeftCollectionViewFlowLayout。 3. 在 ViewDidLoad 中(或任何时候执行初始化)
- (void)viewDidLoad
{
[super viewDidLoad];
GSRightToLeftCollectionViewFlowLayout *layout = [[GSRightToLeftCollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(98, 138); // set the item size.
layout.minimumLineSpacing = 1; // other layout properties.
layout.minimumInteritemSpacing = 1; // other layout properties.
layout.headerReferenceSize = CGSizeMake(50, 18);
layout.sectionInset = UIEdgeInsetsMake(1.0f, 0.0f, 1.0f, 0.0f);
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
// in case you use headers / footers. this is also where you would register a Cell if you don't use storyboards.
[self.collectionView registerClass:[GSReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
self.collectionView.collectionViewLayout = layout;
}
关于iOS UICollectionView - 默认流程,从右到左填充行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19712201/
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option
我有一个驼峰式字符串,例如:JustAString。我想按照以下规则形成长度为4的字符串:抓取所有大写字母;如果超过4个大写字母,只保留前4个;如果少于4个大写字母,则将最后大写字母后的字母大写并添加字母,直到长度变为4。以下是可能发生的3种情况:ThisIsMyString将产生TIMS(大写字母);ThisIsOneVeryLongString将产生TIOV(前4个大写字母);MyString将生成MSTR(大写字母+tr大写)。我设法用这个片段解决了前两种情况:str.scan(/[A-Z]/).first(4).join但是,我不太确定如何最好地修改上面的代码片段以处理最后一种
路由有如下代码:resources:orders,only:[:create],defaults:{format:'json'}resources:users,only:[:create,:update],defaults:{format:'json'}resources:delivery_types,only:[:index],defaults:{format:'json'}resources:time_corrections,only:[:index],defaults:{format:'json'}是否可以使用1个字符串为所有资源设置默认格式,每行不带“默认值”散列?谢谢。
我是ruby的新手,正在配置IRB。我喜欢pretty-print(需要'pp'),但总是输入pp来漂亮地打印它似乎很麻烦。我想做的是默认情况下让它漂亮地打印出来,所以如果我有一个var,比如说,'myvar',然后键入myvar,它会自动调用pretty_inspect而不是常规检查。我从哪里开始?理想情况下,我将能够向我的.irbrc文件添加一个自动调用的方法。有什么想法吗?谢谢! 最佳答案 irb中默认pretty-print对象正是hirb被迫去做。Theseposts解释hirb如何将几乎所有内容转换为ascii表。虽
我正在尝试将一个资源属性的默认值设置为另一个属性的值。我正在为我正在构建的tomcat说明书定义一个资源,其中包含以下定义。我想要可以独立设置的“名称”和“服务名称”属性。当未设置服务名称时,我希望它默认为为“名称”提供的任何内容。以下不符合我的预期:attribute:name,:kind_of=>String,:required=>true,:name_attribute=>trueattribute:service_name,:kind_of=>String,:default=>:name注意第二行末尾的“:default=>:name”。当我在Recipe的新block中引用我
我正在尝试解决http://projecteuler.net/problem=1.我想创建一个方法,它接受一个整数,然后创建一个包含它前面的所有整数的数组,并将整数本身作为数组中的值。以下是我目前所拥有的。代码不起作用。defmake_array(num)numbers=Array.newnumcount=1numbers.eachdo|number|numbers 最佳答案 (1..num).to_a是您在Ruby中需要做的全部。1..num将创建一个Range对象,以1开始并以任意值num结束是。Range对象有to_a方法通过
如果我想要“00001”而不是“1”,除了我自己写填零方法之外,有没有内置的方法可以帮助我为整数填零? 最佳答案 puts"%05d"%1#00001参见:String::%,Kernel::sprintf这是正在发生的事情。%左侧的"%05d"是C风格的格式说明符。%右边的变量就是要格式化的东西。格式说明符可以像这样解码:%-格式说明符的开头0-用前导零填充5-长度为5个字符d-被格式化的是一个整数如果你要格式化多个东西,你会把它们放在一个数组中:"%d-%s"%[1,"One"]#=>1-one