2011-05-28 29 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