0

我有一个错误。我想UIImageView在 special 的单元格上显示一个indexPath.row,但是当我滚动时这些UIImageView重复。示例:我UIImageView在单元格indexPath.row== 0 上显示我,如果我向下滚动,我UIImageView会在indexPath.row== 8 的单元格上看到我的。

这是我的代码:

// 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 = [self getCellContentView:CellIdentifier];

        }

        UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
        UIImageView *imgRead = (UIImageView *)[cell viewWithTag:6];

        [cell.contentView insertSubview:imgRead aboveSubview:lblTemp1];

        contentDictio = [dict objectAtIndex:indexPath.row];

        lblTemp1.text = [contentDictio objectForKey:@"title"];

        NSArray *paths_id = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *basePath_id = ([paths_id count] > 0) ? [paths_id objectAtIndex:0] : nil;
        NSString *path_id = [basePath_id stringByAppendingPathComponent:@"id.plist"];

        NSMutableArray *mut_array_id = [[NSArray arrayWithContentsOfFile:path_id] mutableCopy];

        NSMutableDictionary *c0 = [[NSMutableDictionary alloc] init];

        NSString *idPlistData = [contentDictio objectForKey:@"id"];

        for(c0 in mut_array_id) {
            if([[c0 objectForKey:@"id"] isEqualToString:idPlistData]) {
                [imgRead setImage:[UIImage imageNamed:@"read"]];
            }
            else {
            }
        }
    }

    return cell;
}

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

    CGRect Label1Frame = CGRectMake(kTableCellSmallMargin*2 + 60, kTableCellSmallMargin, 240, 25);

    UILabel *lblTemp;

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                    reuseIdentifier:cellIdentifier] autorelease];

    //Initialize Label with tag 1.
    lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
    lblTemp.tag = 1;
    lblTemp.backgroundColor = [UIColor clearColor];
    [lblTemp setFont: [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:15.0]];
    [cell.contentView addSubview:lblTemp];
    [lblTemp release];

    UIImageView *read=[[UIImageView alloc] initWithFrame:CGRectMake(kTableCellSmallMargin, kTableCellSmallMargin, 60, 60)];
    read.backgroundColor=[UIColor clearColor];
    read.tag = 6;
    [cell.contentView addSubview:read];
    [read release];

    return cell;
}

谢谢...

4

1 回答 1

0

该单元格已缓存,因此当您不需要图像时必须将其清除,如下所示:

    BOOL found = NO;
    for(c0 in mut_array_id) {
        if([[c0 objectForKey:@"id"] isEqualToString:idPlistData]) {
            [imgRead setImage:[UIImage imageNamed:@"read"]];
            found = YES;
        }
        else {

        }
    }

    if (!found)
    {
        [imgRead setImage:nil];
    }
于 2012-11-08T14:32:47.777 回答