条件语句+ UITableView:一遍又一遍地重复数组中的值

时间:2014-10-22 08:59:20

标签: arrays uitableview conditional-statements

我发现很难设置标题,所以请随时修改它。

我有一个数组,其中包含我在CGSizes中使用的cellForRowAtIndexPath

 NSArray *mElements = [NSArray arrayWithObjects:[NSValue valueWithCGSize:
                      CGSizeMake(600.0, 900.0)],
                      [NSValue valueWithCGSize:CGSizeMake(300.0, 800.0)],
                      [NSValue valueWithCGSize:CGSizeMake(546.0, 1032.0)],
                      [NSValue valueWithCGSize:CGSizeMake(300.0, 700.0)],
                      [NSValue valueWithCGSize:CGSizeMake(300.0, 800.0)],
                      nil];

我目前认为从数组中取出一个随机大小并应用于每个单元格,使其呈现交错布局。但问题是如果我重新加载UITableView所有行都会有不同的大小,这很明显,因为它每次都会选择一个随机值。

int random = arc4random_uniform((uint32_t) mElements.count);
CGSize size =[[mElements objectAtIndex:random] CGSizeValue];

然而,我想要的效果是row 0将始终具有相同的大小,可能是数组的索引0。然后row 1将值设置为索引1,然后将第2行设置为索引2处的值,依此类推。然后,我希望索引4处的row具有数组的索引0处的值(因此数组再次启动)。我试过这个,但它需要太多的硬编码。

if (indexPath.item%2 && !indexPath.item%4) {

    size =[[mElements objectAtIndex:1] CGSizeValue];
}
else if(indexPath.item%3 && !indexPath.item%6){
    size =[[mElements objectAtIndex:2] CGSizeValue];

}
else if(indexPath.item%4){

    size =[[mElements objectAtIndex:2] CGSizeValue];

}
//... and so on

我希望在创建动态条件状态时获得帮助,我不必在表中对每个可能的row索引进行硬编码。我似乎无法理解它。

1 个答案:

答案 0 :(得分:1)

size = [[mElements objectAtIndex:indexPath.row % [mElements count]] CGSizeValue];

这会给你一直持续的重复模式。 行数可以是

return [mElements count]*ROW_MULTIPLYER;

,其中

#define ROW_MULTIPLYER 10000
相关问题