UITableView给出了奇怪的结果

问题描述:

我确定这将是某人指出一些真正显而易见的事情之一,即我正在做的事情,但我不能为我的生活找到问题。基本上我有一个字符串数组,并且在需要时将数组中的文本加载到我的uitableviewcells中。问题出现在我开始滚动时,出于某种原因,第6号单元格即第7号单元格显示了数组位置0上的文本从数组位置6的文本顶部显示,而当我向后滚动单元格0中的文本时,或根据(我不能完全告诉)阵列位置6的文本!! ??我不知道我是如何做到这一点的。这里是我的代码:UITableView给出了奇怪的结果

NSMutableArray *titles1 = [[NSMutableArray alloc]initWithCapacity:10]; 
[titles1 insertObject:[NSString stringWithFormat:@"0"] atIndex:0]; 
[titles1 insertObject:[NSString stringWithFormat:@"1"] atIndex:1]; 
[titles1 insertObject:[NSString stringWithFormat:@"2"] atIndex:2]; 
[titles1 insertObject:[NSString stringWithFormat:@"3"] atIndex:3]; 
[titles1 insertObject:[NSString stringWithFormat:@"4"] atIndex:4]; 
[titles1 insertObject:[NSString stringWithFormat:@"5"] atIndex:5]; 
[titles1 insertObject:[NSString stringWithFormat:@"6"] atIndex:6]; 
[titles1 insertObject:[NSString stringWithFormat:@"7"] atIndex:7]; 
[titles1 insertObject:[NSString stringWithFormat:@"8"] atIndex:8]; 
[titles1 insertObject:[NSString stringWithFormat:@"9"] atIndex:9]; 

self.secretTitles = titles1; 
[titles1 release]; 

//自定义表格视图单元格的外观。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
     cell.textLabel.adjustsFontSizeToFitWidth = YES; 
    } 

    // Configure the cell. 
    cell.selectedBackgroundView.backgroundColor = [UIColor clearColor]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    [self configureCell:cell atIndexPath:indexPath]; 

    return cell; 
} 


-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 

    secretName = [[UILabel alloc]initWithFrame:(CGRectMake(10, 8, 100, 30))]; 
    secretName.backgroundColor = [UIColor clearColor]; 
    secretName.textColor = [UIColor whiteColor]; 
    secretName.font = [UIFont boldSystemFontOfSize:18]; 
    secretName.shadowColor = [UIColor colorWithRed:0./255 green:0./255 blue:0./255. alpha:0.7]; 
    secretName.shadowOffset = CGSizeMake(0, 2.0); 
    secretName.text = @""; 
    NSLog(@"row = %d", indexPath.row); 
    secretName.text = [self.secretTitles objectAtIndex:indexPath.row]; 
    [cell.contentView addSubview:secretName]; 
} 

有人可以请我摆脱我的痛苦。非常感谢

朱尔斯

每次使用(重新)使用单元格时,您都会在单元格的内容视图中添加新标签,以便最终获得多个标签,并将它们放在彼此顶部。正确的做法是只添加一次标签,然后设置文本值:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
     cell.textLabel.adjustsFontSizeToFitWidth = YES; 

     UILabel *secretName = [[UILabel alloc]initWithFrame:(CGRectMake(10, 8, 100, 30))]; 
     secretName.backgroundColor = [UIColor clearColor]; 
     secretName.textColor = [UIColor whiteColor]; 
     secretName.font = [UIFont boldSystemFontOfSize:18]; 
     secretName.shadowColor = [UIColor colorWithRed:0./255 green:0./255 blue:0./255. alpha:0.7]; 
     secretName.shadowOffset = CGSizeMake(0, 2.0); 
     secretName.tag = 100; // Arbitrary value that you can use later 
     [cell.contentView addSubview:secretName]; 
     [secretName release]; // Do not forget to release the label! 
    } 

    // Configure the cell. 
    cell.selectedBackgroundView.backgroundColor = [UIColor clearColor]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    UILabel* label = (UILabel*)[cell.contentView viewWithTag:100]; 
    label.text = [self.secretTitles objectAtIndex:indexPath.row]; 

    return cell; 
} 
+0

感谢您的所有帮助..... Vladmir的解决方案已为我工作。我想我需要阅读重新使用单元格!! 非常感谢 Jules – Jules 2010-07-28 15:42:02

您正在为每个单元格添加子视图。单元对象被重用。由于API不知道关于单元格的操作,所以您修改的任何内容,旁边的标准,都是您的责任。

您应该使用cell.textLabel或其他现有UITableViewCell的成员来显示您的数据。

OR:彻底解决了单元复用问题,但这并不是明智的做法。

每次配置单元格时,都会将标签添加为子视图。但是单元可能会被重用并且之前已经配置好。您应该使用现有的标签,如textLabeldetailTextLabel,或者在创建新单元的alloc-and-init之后的单元somwhere时添加标签secretName。然后在configureCell中只设置标签的文字。

您需要了解UITableView重用单元格的方式。代码中发生的事情是,您已将secretName添加到已拥有它的单元中。这里是你的代码的快速重写:

//we need a way to find the secretName UILabel in the cell's contentView 

enum { 
    kSecretNameTag = 255 
}; 

(UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
     // we only need to set up the cell when we create it 
     cell.textLabel.adjustsFontSizeToFitWidth = YES; 
     cell.selectedBackgroundView.backgroundColor = [UIColor clearColor]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     [self configureCell:cell]; 
    } 
    // grab the secretName UILabel and set the text 
    UILabel* secretName = [cell viewWithTag:kSecretNameTag]; 
    secretName.text = @""; 
    NSLog(@"row = %d", indexPath.row); 
    secretName.text = [self.secretTitles objectAtIndex:indexPath.row]; 
    return cell; 
} 

(void)configureCell:(UITableViewCell *)cell { 
    secretName = [[UILabel alloc]initWithFrame:(CGRectMake(10, 8, 100, 30))]; 
    secretName.backgroundColor = [UIColor clearColor]; 
    secretName.textColor = [UIColor whiteColor]; 
    secretName.font = [UIFont boldSystemFontOfSize:18]; 
    secretName.shadowColor = [UIColor colorWithRed:0./255 green:0./255 blue:0./255. alpha:0.7]; 
    secretName.shadowOffset = CGSizeMake(0, 2.0); 
    secretName.tag = kSecretNameTag; 
    [cell.contentView addSubview:secretName]; 
}