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

发布于 2024-11-10 06:39:52 字数 4439 浏览 1 评论 0原文

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

- (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 (选择查看按钮) 为什么?

I have 2 tables in coredata sqlite Painter and Picture. With relationship one-to-many.
In table "picture" I have string attribute pictureName. I store pictures(153) on a disk
This code I add imageViews to cells:

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

`
And I have problem: a lot of photo in every cell
http://ge.tt/9QX5lc4?c
(select view button)
Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆凄美了谁 2024-11-17 06:39:52

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

Cells are being reused. Every time you are configuring a cell, you are adding an image view as a subview. Over time these accumulate and are providing the effect you are seeing. You should check if an image view already exists and assign an image to it or clean up the cell first and then set it up with an image.

羅雙樹 2024-11-17 06:39:52

以下代码是导致您出现问题的原因:

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;

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

The following code is what is causing the problem for you:

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;

You're basically caching the cell the first time it's created and re-using it on subsequent calls to tableView:cellForRowAtIndexPath: If you don't want this to be cached then you'll need to create a new cell every time.

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

This should fix your issue. There are more elaborate ways of handling cells such as caching it and manipulating its subviews directly instead of recreating it every time. Something to keep in mind as you continue to work on your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文