2012-08-27 76 views
0

我有一個2行的表,每列3列,一個數組有5個值。這些值應該連續顯示在3列中,例如用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個值。 我與計算struggeling左右,這些不適合與數組的索引...

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

BR, mybecks

回答

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} 
} 
0

假設COLS是columss的數量,V是值的數量,其中M是矩陣,Vector是值向量:

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