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 に答える