2011-05-28 60 views
1

我在coredata sqlite Painter和Picture中有2个表格。关系一对多。 在表“图片”我有字符串属性pictureName。我存储在磁盘 此代码,我添加imageViews细胞上的图像(153):在UITableView单元中重叠图像的问题

- (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

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

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; 

这应该解决您的问题。有更多精细的处理单元格的方法,例如直接缓存它并直接操作它的子视图,而不是每次重新创建它。在继续处理代码时需要注意的事项。

+0

这会让它变得迟钝。 – 2011-05-28 19:49:17

+0

是的,的确如此。这就是为什么我指出有更复杂的解决方案涉及操纵缓存单元格的子视图。 – csano 2011-05-28 20:33:40

+0

Laggy?我已经使用这个解决方案,它解决了我的问题。我没有注意到滞后,但可能这是一件坏事 – 2011-05-29 09:35:05