проблема с uitableviewcell - PullRequest
       11

проблема с uitableviewcell

0 голосов
/ 01 декабря 2009

Я использую настроенную ячейку, чтобы показать изображение. Но когда я добавляю изображение в первый ряд, оно загружается в каждый четвертый ряд. В чем может быть проблема? Я выбираю изображение из библиотеки фотографий iphone с помощью uiimagepickercontroller и передаю его первой ячейке таблицы.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    CGSize newSize = CGSizeMake(80, 80);
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    imageView.image = newImage;

    [picker dismissModalViewControllerAnimated:YES];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
        for (id currentObject in nib){
            if ([currentObject isKindOfClass:[CustomCell class]]){
                cell = (CustomCell *)currentObject;
                //[cell loadFullComments:[latestFMLComments objectAtIndex:indexPath.row]];
                break;
            }
        }
    }

    NSUInteger r= [indexPath row];
    NSUInteger s= [indexPath section];
    //[cell setText:[NSString stringWithFormat:@"I am cell %d", indexPath.row]];
    // NSInteger r= indexPath.row;
    if(r == 1)
        if (s== 0){
             UIImage *img=imageView.image;
             cell.imageView.image = img;
        }
     return cell;
}

1 Ответ

1 голос
/ 01 декабря 2009

iPhone SDK UITableView оптимизирует создание UITableViewCell путем повторного использования ячеек, которые не отображаются, вместо того, чтобы постоянно создавать новые. Это делается через ваш звонок dequeueReusableCellWithIdentifier:.

В tableView:cellForRowAtIndexPath: необходимо убедиться, что очищаемая ячейка очищается, чтобы она содержала только новое содержимое ячейки. В этом случае достаточно cell.imageView.image = nil перед вашим if (r == 1) ... оператором.

...