2010-02-16 55 views
1

我的代码中有一个名为“conteggio”的变量,你可以在下面看到它......这个变量必须在我的tableview的每一行增加1 ...当我尝试这样做时,我收到如下结果: 4,8,12,16等。每行的倍数为4 ......似乎它为每行重复了4次代码。为什么tableview中的代码每行重复4次?

如果我来回滚动我的表格,这些数字会变成倍数。

这里是我的代码:

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"]; 

if (cell == nil){ 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cellID"] autorelease]; 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellID"]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
} 


NSString *alphabet = [fevIndice objectAtIndex:[indexPath section]]; 

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet]; 
NSArray *fv = [fev filteredArrayUsingPredicate:predicate]; 


conteggio++; 


NSString *string = [NSString stringWithFormat:@"%d", conteggio]; 

cell.detailTextLabel.text = string; 


if ([fv count]>0) { 

    NSString *cellValue = [fv objectAtIndex:indexPath.row]; 

    cell.textLabel.text = cellValue; 

    //indexPath.section; //[fv count] numero di elementi in una section; 
    //cell.detailTextLabel.text = [fevMesi objectAtIndex:conteggio]; 
    //cell.imageView.image = [UIImage imageNamed:[fevIcona objectAtIndex:]]; 

} 

cell.detailTextLabel.numberOfLines = 3; 
cell.detailTextLabel.font = [UIFont systemFontOfSize:11.0]; 

return cell; 

}

回答

0

有关于多少次法会要求每个渲染没有合同。请问indexPath它的终端索引。

1

使用indexPath.row获取它所调用的行号。就像乔纳森说的那样,这种方法可以被称为任意次数,所以不要试图自己追踪这一行。

0

正如其他人已经提到的,只要表视图需要显示单元格(例如,当它滚动到视图中),cellForRowAtIndexPath就可以为每行调用多次。

你的表格视图中有多个部分吗?
是否应该将这个“绝对”唯一的行数视为不管该段?

如果每个部分具有相同的行数,那么这是一个计算绝对行数的简单公式。

但是,如果每个部分具有行的不同的充数,你可以通过执行类似下面的计算绝对行号:

conteggio = 0; 
for (int s = 0; s < indexPath.section; s++) 
{ 
    conteggio += [tableView numberOfRowsInSection:s]; 
} 
conteggio += indexPath.row + 1; 

如果可能的话,它可能是更好地为您重新设计fevMesi如此那里的对象可以使用行和段来检索,而不必每次都计算绝对行号。

+0

我有部分,但我没有相同数量的行每个部分..其实这是问题...您的解决方案似乎是我的权利...我要去尝试它然后我让你知道! – Chris 2010-02-16 14:41:11

+0

很棒!它工作正常。感谢你的回答。有了这个我已经完成了我的申请,所以我非常感谢你! – Chris 2010-02-16 15:19:50

+0

我很高兴它的工作。请点击支票将其标记为答案。谢谢。 – DyingCactus 2010-02-16 15:38:47

相关问题