uitableview - пользовательские изображения продолжают загружаться в cell.contentView - PullRequest
0 голосов
/ 23 сентября 2010

В cellForRowAtIndexPath Я добавляю UIImageView к cell.contentView. Проблема в том, что когда ячейка прокручивается за пределы экрана и снова включается, она снова добавляет то же изображение поверх уже существующего. Это происходит непрерывно, пока я не получу очень сложное размытое изображение.

Вам нужно продолжать удалять какие-либо изображения, которые вы добавляете в cell.contentView? Если да, то в каком методе делегата вы это делаете?

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

    static NSString *CellIdentifier = @"CellIdentifier";

    MyTableCell *cell = (MyTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.jpg"]];

    imageView.center = CGPointMake(310, 48);
    [cell.contentView addSubview:imageView];
    [imageView release];
    return cell;
}

Ответы [ 2 ]

3 голосов
/ 23 сентября 2010

Если вы не хотите помещать imageViews в свою ячейку, вам нужно выполнить всю настройку внутри блока if(cell==nil), в противном случае он будет добавляться по одному при каждой перезагрузке ячейки. При использовании утилизации ячеек вы всегда хотите сохранить все, что соответствует для всех ваших ячеек в этом блоке, чтобы они добавлялись только один раз.

Ex:

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

    static NSString *CellIdentifier = @"CellIdentifier";

    MyTableCell *cell = (MyTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

        //Add custom objects to the cell in here!
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.jpg"]];

        imageView.center = CGPointMake(310, 48);
        [cell.contentView addSubview:imageView];
        [imageView release];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;


    return cell;
}
1 голос
/ 23 сентября 2010

Если каждая ячейка должна иметь свое изображение, вы можете попробовать что-то вроде этого:

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

    static NSString *CellIdentifier = @"CellIdentifier";
    static const int ImageViewTag = 1234; //any integer constant

    MyTableCell *cell = (MyTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UIImageView *imageView;
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

        //Add custom objects to the cell in here!
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, imgWidth, imgHeight)];

        imageView.center = CGPointMake(310, 48);
        imageView.tag = ImageViewTag;
        [cell.contentView addSubview:imageView];
        [imageView release];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    else
    {
        imageView = [cell viewWithTag:ImageViewTag];
    }
    imageView.image = yourUIImageForThisCell;

    return cell;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...