【问题标题】:Issue with overlapping images in UITableView cellsUITableView 单元格中重叠图像的问题
【发布时间】:2011-05-28 19:12:29
【问题描述】:

我在 coredata sqlite Painter 和 Picture 中有 2 个表。具有一对多的关系。 在表“图片”中,我有字符串属性图片名称。我将图片(153)存储在磁盘上 这段代码我将 imageViews 添加到单元格中:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    self.tableView.transform = CGAffineTransformMakeRotation( -M_PI/2 );
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView setFrame:CGRectMake(0, 156, 1024, 449)];
}

- (void)viewDidUnload
{
    [self setTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}
#pragma mark - UITableView Delegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[fetchedResultsController fetchedObjects] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    Painter *painter = [[fetchedResultsController fetchedObjects] objectAtIndex:section];
    //NSLog(@"%i", [painter.pictures count]);
    return [painter.pictures count];
    //return 152;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    Painter *painter = [[fetchedResultsController fetchedObjects] objectAtIndex:indexPath.section];
    Picture *picture = [[painter.pictures allObjects] objectAtIndex:indexPath.row];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@s.jpg", picture.imgName]]];
    NSLog(@"add image %@s.jpg to sector:%i row:%i", picture.imgName, indexPath.section, indexPath.row);
    imageView.transform = CGAffineTransformMakeRotation( M_PI/2 );
    [cell addSubview:imageView];
    [imageView release];
}

- (UITableViewCell *)tableView:(UITableView *)tableViewCurrent cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableViewCurrent dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}
- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 300.0;
}

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ 
}


#pragma mark - Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController
{
    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }

    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Painter" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];


    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Main"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];

    return fetchedResultsController;
}

` 我有问题:每个单元格中都有很多照片 http://ge.tt/9QX5lc4?c (选择查看按钮) 为什么?

【问题讨论】:

    标签: objective-c ios uitableview core-data uiimageview


    【解决方案1】:

    细胞正在被重复使用。每次配置单元格时,都会将图像视图添加为子视图。随着时间的推移,这些积累并提供您所看到的效果。您应该检查图像视图是否已经存在并为其分配图像或先清理单元格然后使用图像进行设置。

    【讨论】:

      【解决方案2】:

      以下代码是您遇到问题的原因:

      static NSString *CellIdentifier = @"Cell";
      
      UITableViewCell *cell = [tableViewCurrent dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
          cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
      }
      // Configure the cell.
      [self configureCell:cell atIndexPath:indexPath];
      return cell;
      

      您基本上是在第一次创建单元格时对其进行缓存,并在随后对tableView:cellForRowAtIndexPath: 的调用中重新使用它。如果您不希望缓存此单元格,那么您每次都需要创建一个新单元格。

      UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
      // Configure the cell.
      [self configureCell:cell atIndexPath:indexPath];
      return cell;
      

      这应该可以解决您的问题。有更精细的方法来处理单元格,例如缓存它并直接操作它的子视图,而不是每次都重新创建它。在您继续编写代码时要记住一些事情。

      【讨论】:

      • 是的,这是真的。这就是为什么我指出有更精细的解决方案涉及操纵缓存单元格的子视图。
      • 懒惰?我已经使用了这个解决方案,它解决了我的问题。我没有注意到滞后,但这可能是一件坏事
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多