3

我在实现中使用了 Cell.ContentView 来自定义单元格内容。它工作正常,但唯一的问题是当我有很多单元格并且我上下滚动它们时,单元格内容被覆盖到单元格中,然后隐藏然后可见。假设我向上滚动第一个单元格然后再次将其取下,最后一个单元格的内容被第一个单元格覆盖!

我对此进行了足够的调试,但找不到确切的解决方案。我尝试检查 Cell.ContentView.SubViews 计数,如果它为 0,则仅添加其他子视图。在我上下滚动它们之前,这不会显示任何单元格内容,但是一旦出现内容,它就不会覆盖..有点奇怪..!!我还确保我正确地重复使用了单元格。以下是我将子视图添加到单元格内容视图中的代码。请让我知道我怎样才能摆脱这个问题。

PS:不用担心我做过的变量和计算。假设它返回正确的值。:)

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    NSInteger totalAvailableSpace = IPHONE_DISPLAY_WIDTH - iconSize - accesorySize - 10;
    NSInteger lableHeight = [[cellDetails objectForKey:@"TableItemTextFontSize"] intValue] * 2 + 10;

    UILabel *textLabel = nil;
    textLabel = [[[UILabel alloc] initWithFrame:CGRectMake(iconSize+12, self.tableCellHeight/2 - lableHeight/2, totalAvailableSpace * 0.8, lableHeight)] autorelease];
    textLabel.numberOfLines = 2;
    textLabel.lineBreakMode = UILineBreakModeWordWrap;
    textLabel.textAlignment = UITextAlignmentLeft;
    textLabel.text = [cellDetails objectForKey:@"TableItemMainText"];
    if ([textLableColor scanHexInt:&hex]) {
        textLabel.textColor = UIColorFromRGB(hex);
    }
    textLabel.font = [UIFont fontWithName:[cellDetails objectForKey:@"TableItemTextFontName"] size:[[cellDetails objectForKey:@"TableItemTextFontSize"] intValue]]; 
    [cell.contentView addSubview:textLabel];
    textLabel.backgroundColor = [UIColor clearColor];

    lableHeight = [[cellDetails objectForKey:@"TableItemDetailTextFontSize"] intValue] * 2 + 10;
    UILabel *detailTextLabel = nil;
    detailTextLabel = [[[UILabel alloc] initWithFrame:CGRectMake(iconSize+10+totalAvailableSpace * 0.8+5, self.tableCellHeight/2 - lableHeight/2, totalAvailableSpace * 0.2 - 10, lableHeight)] autorelease];
    detailTextLabel.numberOfLines = 2;
    detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
    detailTextLabel.textAlignment = UITextAlignmentLeft;
    detailTextLabel.text = [cellDetails objectForKey:@"TableItemDetailText"];
    if ([detailTextLableColor scanHexInt:&hex]) {
        detailTextLabel.textColor = UIColorFromRGB(hex);
    }
    detailTextLabel.font = [UIFont fontWithName:[cellDetails objectForKey:@"TableItemDetailTextFontName"] size:[[cellDetails objectForKey:@"TableItemDetailTextFontSize"] intValue]];
    [cell.contentView addSubview:detailTextLabel];
    detailTextLabel.backgroundColor = [UIColor clearColor];

    return cell;
}

谢谢。

4

5 回答 5

6

那是因为玩具一遍又一遍地将视图添加到您的单元格中。

您应该只在创建单元格时添加它们,并在重复使用单元格时设置标签。

您可以为标签设置标签,以便之后设置文本。下面的代码可以解决问题

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        if(style == UITableViewCellStyleValue1)
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        else
            cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:CellIdentifier] autorelease];


        NSInteger totalAvailableSpace = IPHONE_DISPLAY_WIDTH - iconSize - accesorySize - 10;
        NSInteger lableHeight = [[cellDetails objectForKey:@"TableItemTextFontSize"] intValue] * 2 + 10;

        UILabel *textLabel = nil;
        textLabel.tag = 1;
        textLabel = [[[UILabel alloc] initWithFrame:CGRectMake(iconSize+12, self.tableCellHeight/2 - lableHeight/2, totalAvailableSpace * 0.8, lableHeight)] autorelease];
        textLabel.numberOfLines = 2;
        textLabel.lineBreakMode = UILineBreakModeWordWrap;
        textLabel.textAlignment = UITextAlignmentLeft;
        textLabel.text = [cellDetails objectForKey:@"TableItemMainText"];
        if ([textLableColor scanHexInt:&hex]) {
            textLabel.textColor = UIColorFromRGB(hex);
        }
        textLabel.font = [UIFont fontWithName:[cellDetails objectForKey:@"TableItemTextFontName"] size:[[cellDetails objectForKey:@"TableItemTextFontSize"] intValue]]; 
        [cell.contentView addSubview:textLabel];
        textLabel.backgroundColor = [UIColor clearColor];

        lableHeight = [[cellDetails objectForKey:@"TableItemDetailTextFontSize"] intValue] * 2 + 10;
        UILabel *detailTextLabel = nil;
        detailTextLabel.tag = 2;
        detailTextLabel = [[[UILabel alloc] initWithFrame:CGRectMake(iconSize+10+totalAvailableSpace * 0.8+5, self.tableCellHeight/2 - lableHeight/2, totalAvailableSpace * 0.2 - 10, lableHeight)] autorelease];
        detailTextLabel.numberOfLines = 2;
        detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
        detailTextLabel.textAlignment = UITextAlignmentLeft;
        detailTextLabel.text = [cellDetails objectForKey:@"TableItemDetailText"];
        if ([detailTextLableColor scanHexInt:&hex]) {
            detailTextLabel.textColor = UIColorFromRGB(hex);
        }
        detailTextLabel.font = [UIFont fontWithName:[cellDetails objectForKey:@"TableItemDetailTextFontName"] size:[[cellDetails objectForKey:@"TableItemDetailTextFontSize"] intValue]];
        [cell.contentView addSubview:detailTextLabel];
        detailTextLabel.backgroundColor = [UIColor clearColor];

    } else {

        UILabel *textLabel = (UILabel *)[cell viewWithTag:1];
        textLabel.text = [cellDetails objectForKey:@"TableItemMainText"];

        UILabel *detailTextLabel = (UILabel *)[cell viewWithTag:2];
        detailTextLabel.text = [cellDetails objectForKey:@"TableItemDetailText"];

    }

    return cell;
}
于 2011-06-16T11:13:12.263 回答
4
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
}

确保dequeueReusableCellWithIdentifier并且reuseIdentifier应该是nil

现在它将起作用!

于 2012-10-03T10:08:50.333 回答
2

将所有单元格 UI 内容放在 if(cell==nil){uilabel,uilabel or anythingUI related} ...因为在创建 cel 时 ui 应该只调用一次

于 2011-06-16T11:20:44.277 回答
2

需要明确的是,您的问题是,当您将单元格滚动到视图中时,您经常会发现新单元格的文本被写在刚刚滚动到屏幕外的单元格的顶部?

问题是它dequeueReusableCellWithIdentifier:没有“清理”单元格(它调用prepareForReuse,但就是这样),所以你所有的旧子视图仍然存在。如果您要重用单元格,则不应每次都重新创建这些子视图。相反,只需调整您从中返回的单元格上现有子视图的属性dequeueReusableCellWithIdentifier:,并仅在新分配的单元格上创建新子视图。子类化 UITableViewCell 可以帮助解决这个问题,既可以提供属性/变量来保存对这些子视图的引用,又可以通过将子视图的创建和更新移动到适当的方法中来更好地组织代码。或者,您可以通过将 nil 传递给重用标识符来不重用单元格。

在您上下滚动之前它不显示任何内容的最可能原因是,新创建的单元格可能具有框架提供的文本字段和图像视图,如果框架确定您不在,则可能会将其删除t 实际使用它们。

于 2011-06-16T11:21:11.427 回答
0

在 iOS5UILineBreakModeWordWrap中已弃用。你应该NSLineBreakByWordWrapping改用。

这会将您的代码更改为detailTextLabel.lineBreakMode = NSLineBreakByWordWrappingtextLabel.lineBreakMode = NSLineBreakByWordWrapping.

于 2013-06-26T15:55:11.070 回答