0

我的UITableView单元格从其他单元格中获取内容(图像)。我不知道为什么会这样,但我确信我的错误是愚蠢的。在下图中,您可以看到它的外观。顶部的单元格从底部的单元格中获取图像(facebook 和 twitter 图标)。

这是我的cellForRowAtIndexPath 代码:

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

    NSDictionary *dictionary = [mainArray objectAtIndex:indexPath.section];
    NSArray *array1 = [dictionary objectForKey:@"stuff"];

    NSString *cellValue = [array1 objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue; // загаловок клетки
    cell.textLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:16]; 
    cell.textLabel.textColor = [UIColor colorWithRed:145/256.0 green:0/256.0 blue:0/256.0 alpha:1.0];    
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.textLabel.textAlignment = UITextAlignmentLeft;

    if (indexPath.section == 0 && indexPath.row == 1)
    {
        if([[NSUserDefaults standardUserDefaults]boolForKey:@"password"] == YES)
        {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    }

    switch(indexPath.section)
    {

        case 2:
        {
            cell.textLabel.textAlignment = UITextAlignmentCenter;
        }
        break;

        case 3:
        {
            if (indexPath.row == 0) 
                cell.imageView.image = [UIImage imageNamed:@"facebook"];
            else if (indexPath.row == 1)
                cell.imageView.image = [UIImage imageNamed:@"twitter"];
            else if (indexPath.row == 2)
                cell.imageView.image = [UIImage imageNamed:@"youtube"];
            else
                cell.imageView.image = [UIImage imageNamed:@"vk"];
        }
        break;
            default:
            break;
    }




    return cell;
}

在此处输入图像描述

我该如何解决?

4

1 回答 1

1

单元格被重复使用,因此当您不希望出现图像时,必须将单元格的 imageView 设置为 nil。尝试这个:

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

    NSDictionary *dictionary = [mainArray objectAtIndex:indexPath.section];
    NSArray *array1 = [dictionary objectForKey:@"stuff"];

    NSString *cellValue = [array1 objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue; // загаловок клетки
    cell.textLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:16]; 
    cell.textLabel.textColor = [UIColor colorWithRed:145/256.0 green:0/256.0 blue:0/256.0 alpha:1.0];    
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.textLabel.textAlignment = UITextAlignmentLeft;
    cell.imageView.image = nil;

    if (indexPath.section == 0 && indexPath.row == 1)
    {
        if([[NSUserDefaults standardUserDefaults]boolForKey:@"password"] == YES)
        {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    }

    switch(indexPath.section)
    {

        case 2:
        {
            cell.textLabel.textAlignment = UITextAlignmentCenter;
        }
        break;

        case 3:
        {
            if (indexPath.row == 0) 
                cell.imageView.image = [UIImage imageNamed:@"facebook"];
            else if (indexPath.row == 1)
                cell.imageView.image = [UIImage imageNamed:@"twitter"];
            else if (indexPath.row == 2)
                cell.imageView.image = [UIImage imageNamed:@"youtube"];
            else
                cell.imageView.image = [UIImage imageNamed:@"vk"];
        }
        break;
        default:
        break;
    }




    return cell;
}
于 2012-09-23T11:23:20.413 回答