UITableView给出奇怪的结果 [英] UITableView giving strange results

查看:56
本文介绍了UITableView给出奇怪的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我肯定这将是其中有人指出我确实在做的事情很明显但我一生无法发现问题的事情之一。基本上,我有一个字符串数组,并在需要时将数组中的文本加载到uitableviewcells中。当我开始滚动并且由于某种原因第6号单元格出现问题时,即第7个单元格从数组位置6的文本顶部开始显示数组位置0的文本,并且当我向上滚动回零单元格中的文本时还是在数组位置6的文本下(我不太清楚)!!我不知道我该怎么做。这是我的代码:

Im sure this is going to be one of those things where someone points out something really obvious that Im doing but I cant for the life of me find the problem. Basically I have an array of strings and I am loading the text from the array into my uitableviewcells as and when it is needed. The problem comes when I begin to scroll and for some reason cell no.6 ie the 7th cell displays the text from array position 0 over the top of the text from array position 6 and when I scroll back up the text in cell 0 is behind or under (i cant quite tell) the text from array position 6!!?? I have no idea how I am doing this. Here is my code:

    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];

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

// Customize the appearance of table view cells.

-(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];
}

有人可以让我摆脱困境。非常感谢

Can someone please put me out of my misery. Many thanks

Jules

推荐答案

您要向其中添加新标签每次(重复)使用单元格的内容视图时,最终您会获得多个相互重叠的标签。正确的方法是只添加一次标签,然后为其设置文本值:

You're adding new label to cell's content view each time cell is (re-)used so eventually you get several labels placed on top of each other. The correct approach is to add a label only once and then set text value to it:

-(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;
}

这篇关于UITableView给出奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆