用3列填充表格行和值

时间:2012-08-27 15:50:33

标签: ios algorithm modulo

我有一个包含2行的表,每行3列,还有一个包含5个值的数组。这些值应连续显示在3列中,例如

indexPath.row 0: Array[0] | Array[1] | Array[2]
indexPath.row 1: Array[3] | Array[4] | 

我想创建一些编码,它将我的值(它不关心多少,例如5,6,7,...,20,21,行中3列的格式)行计算在带有值的数组的大小。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{    
    NSMutableArray *values = [... getValues];
    int no=0;
    if (values) {
        no=(values.count+1)/3;
    }
    else{
        no=0;
    }
    return no;
}

我目前的问题是,我没有在列中分配5个值。 我正在计算,这些不适合数组的索引......

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//order values in 3 columns and 2 rows (currently)...
}

BR, mybecks

2 个答案:

答案 0 :(得分:0)

假设COLS是columss的数量,V是值的数量,其中M是矩阵,Vector是值向量:

for i = 0 to V-1:
    M[i div COLS][i mod COLS] = Vector[i]

答案 1 :(得分:0)

找到解决方案。

NSInteger count = numberOfValues;
NSInteger start = indexPath.row * 3;
NSInteger end = MIN(start + 3, count);

for(int i = start; i < end; i++)
{
  if(i%3 == 0){//left}
  if(i%3 == 1){//center}
  if(i%3 == 2){//right}
}