UITableView单元格中重叠图像的问题

时间:2011-05-28 19:12:29

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

我在coredata sqlite Painter和Picture中有2个表。与一对多的关系。 在表“图片”中我有字符串属性pictureName。我将图片(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 (选择查看按钮) 为什么呢?

2 个答案:

答案 0 :(得分:2)

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

答案 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;

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