2013-05-07 144 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框架,因为它会自动处理显示您的阵列。