我在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 (选择视图按钮) 为什么呢?
这会让它变得迟钝。 – 2011-05-28 19:49:17
是的,的确如此。这就是为什么我指出有更复杂的解决方案涉及操纵缓存单元格的子视图。 – csano 2011-05-28 20:33:40
Laggy?我已经使用这个解决方案,它解决了我的问题。我没有注意到滞后,但可能这是一件坏事 – 2011-05-29 09:35:05