2013-05-07 109 views
2

我有6段在一個UITableView,每一部分顯示2個格,通常情況下,我希望它是這樣的: enter image description here重複的元素

然而,這裏是我有: http://i.imgur.com/iGoMpTK.png

每個indexPath.row都在每個部分中重複。 下面是代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"avenir"; 
    Avenir *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(!cell) { 
     cell =[[Avenir alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.equipea.text=[arrayofClubA objectAtIndex:indexPath.section]; 

    cell.equipeb.text=[arrayofClubB objectAtIndex:indexPath.section ]; 


return cell; 

}

的元件被從兩個NSMutableArrays,一個用於在小區中的第一左側元件和另一個用於右元素細胞檢索。 有什麼不對?

謝謝你的幫助。

+0

+1包括你的貓的圖畫屏幕截圖 – matt 2013-05-07 18:03:08

+0

@matt:LOL!你明白我的問題的重要之處在於:D。 – androniennn 2013-05-07 19:11:32

回答

3

您需要計算rowcolumn的正確索引。由於每部分有兩對行,因此需要將section乘以2,然後添加row,這將是零或一。最終的結果應該是這樣的:

NSUinteger pos = indexPath.section*2 + indexPath.row; 
cell.equipea.text=[arrayofClubA objectAtIndex:pos]; 
cell.equipeb.text=[arrayofClubB objectAtIndex:pos]; 
+0

如果沒有你的幫助,我永遠不會知道。數百萬的感謝。 2天有問題:/。非常感謝你。 – androniennn 2013-05-07 17:57:36

+0

如果每個部分的行數不同(如發佈的問題不是2),該怎麼辦,請在此處查看:http://stackoverflow.com/questions/17299453/wrong-cells-values-of-uitableview-per -section/17300044?noredirect = 1#17300044謝謝你回答。 – androniennn 2013-06-25 15:44:49

0

你總是獲取每個部分相同的文字,因爲

cell.equipea.text=[arrayofClubA objectAtIndex:indexPath.section]; 

總是返回相同的值的每個部分(因爲indexPath.section包含部分的索引)。也許你想對以下事情做些什麼?

cell.equipea.text=[arrayofClubA objectAtIndex:indexPath.row]; 

而且,對於這些類型的用途,這可能是很多更直截了當地使用免費的明智的TableView框架,因爲它會自動處理顯示您的陣列。