UITableView 错误的图像加载了自定义单元格和 UIView

Posted

技术标签:

【中文标题】UITableView 错误的图像加载了自定义单元格和 UIView【英文标题】:UITableView wrong images loaded with custom cell and UIView 【发布时间】:2013-10-24 07:12:18 【问题描述】:

我正在开发的应用程序有问题。

我制作了一个带有自定义单元格的 UITableView。在单元格中,我使用 bezierPathWithRoundedRect: 方法放置了一个小 UIView 来显示用户的图像(从 Data Core 加载)

当我在应用程序中加载表格时,图像是正确的,直到我在表格上快速滚动。我认为图像的异步加载/编辑存在问题,但我不知道如何解决。如果我在单元格中使用简单的 UIImageView 而不是 UIView,则一切正常。 我在这里发现了非常相似的问题,但没有 UIView 类和数据核心加载...

这是我的单元格的代码:

- (UITableViewCell *)tableView:(UITableView *)presentTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    static NSString *cellIdentifier = @"FellowshipCell";
    UITableViewCell *cell = [presentTableView dequeueReusableCellWithIdentifier:cellIdentifier];

    ExpenseParticipants *participant = [self.fetchedResultsController objectAtIndexPath:indexPath];

    IconView *icon = (IconView *)[cell.contentView viewWithTag:1];
    icon.immagine = [UIImage imageWithData:participant.usrImage];


    return cell;

这是 UIView 类(IconView.h 和 IconView.m)的实现,用于绘制带有用户图像的圆形矩形:

- (void)drawRect:(CGRect)rect

    // Draving code
    UIBezierPath *icon = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:12];

    [[UIColor greenColor] setFill];
    UIRectFill(self.bounds);

    [immagine drawInRect:self.bounds];

感谢您的帮助!

【问题讨论】:

【参考方案1】:

尝试添加 [inmagine setNeedsDisplay] 以强制重绘您的自定义视图

【讨论】:

非常感谢!那行得通!我添加了 [icon setNeedsDesplay];在返回 Cell 之前。 完成。抱歉耽搁了:) 再次感谢您!【参考方案2】:

也许你应该在单元格为空时重新分配单元格

UITableViewCell *cell = [presentTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil)
    cell = [[UITableView alloc] initWith...]

【讨论】:

以上是关于UITableView 错误的图像加载了自定义单元格和 UIView的主要内容,如果未能解决你的问题,请参考以下文章