Ios UICollectionView单元格绘制左上角

Ios UICollectionView单元格绘制左上角,ios,objective-c,uicollectionview,uicollectionviewcell,Ios,Objective C,Uicollectionview,Uicollectionviewcell,这是我第一次使用UICollectionView 据我所知,我已经把一切都安排好了。我对UICollectionView的出列函数的工作方式有点困难,但我想我已经克服了这一点。当我不知道是调用initWithFrame还是prepareforuse时,设置自定义单元格类是一件棘手的事情 我很确定问题出在prepareforeuse函数中,但问题出在哪里呢 发生的情况是,这些单元格显然会在集合视图的左上角随机绘制,而一些单元格将不在网格中它们所属的位置。(见附图) 当弹跳、滚动和缩放(从而导致重用

这是我第一次使用UICollectionView

据我所知,我已经把一切都安排好了。我对UICollectionView的出列函数的工作方式有点困难,但我想我已经克服了这一点。当我不知道是调用initWithFrame还是prepareforuse时,设置自定义单元格类是一件棘手的事情

我很确定问题出在prepareforeuse函数中,但问题出在哪里呢

发生的情况是,这些单元格显然会在集合视图的左上角随机绘制,而一些单元格将不在网格中它们所属的位置。(见附图)

当弹跳、滚动和缩放(从而导致重用)时,问题就会发生。一张幻灯片将随机出现在左上角,其他幻灯片将随机从网格中消失

(我需要更多的代表来发布图片。呃:|如果你能帮助我,我会通过电子邮件将图片发送给你。bmantzey@mac.com)

我使用静态方法设置幻灯片对象,其中包含准备异步下载或从磁盘缓存检索映像所需的数据

很简单:

+(void)prepareWithSlide:(Slide*)slide{
if(s_slide)
    [s_slide release];

s_slide = [slide retain];}
我不确定这样做是否是一个很大的“不”,但在我的自定义单元格类中,我在initWithFrame块中调用prepareForReuse,因为我需要相同的设置代码:

-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self)
{
    [self prepareForReuse];
}
return self;}
以下是prepareforeuse函数:

-(void)prepareForReuse{
CGSize size = [SpringboardLayout currentSlideSize];
[self setFrame:CGRectMake(0, 0, size.width, size.height)];
self.size = size;

// First remove any previous view, so as not to stack them.
if(_builderSlideView)
{
    if(_builderSlideView.slide.slideID == s_slide.slideID)
        return;

    [_builderSlideView release];
}

for(UIView* aView in self.contentView.subviews)
{
    if([aView isKindOfClass:[BuilderSlideView class]])
    {
        [aView removeFromSuperview];
        break;
    }
}

// Then setup the new view.
_builderSlideView = [[BuilderSlideView alloc] initWithSlide:s_slide];
self.builderCellView = _builderSlideView;
[s_slide release];
s_slide = nil;

[self.contentView addSubview:_builderSlideView];

if([SlideCache isImageCached:_builderSlideView.slide.slideID forPresentation:_builderSlideView.slide.presentationID asThumbnail:YES])
{
    [_builderSlideView loadImageFromCache];
}
else
{
    [_builderSlideView loadView];
}}
最后,下载幻灯片图像后,会发布一条通知(我计划将其更改为代理调用)。通知只是重新加载已接收更新的单元格。以下是通知代码:

-(void)didLoadBuilderCellView:(NSNotification*)note{
BuilderCellView* cellView = [[note userInfo] objectForKey:@"cell"];
BuilderSlideView* slideView = (BuilderSlideView*)cellView;
NSIndexPath* indexPath = [self indexPathForSlide:slideView.slide];
if(indexPath)
    [self.collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];}
请注意,幻灯片对象存在于模型中


关于是什么导致了这个问题,有什么想法吗?提前谢谢

导致单元格左上角绘制和/或消失的问题是由背景线程上的无限递归引起的。简单地说,我没有正确、安全地实现延迟加载。为了解决这个问题,我回到绘图板上,再试一次。正确的延迟加载算法实现了这一点。

对不起,我忘了提到loadView会将幻灯片排队等待下载。图像的接收将缓存图像并发布通知。再次感谢。有人打电话给initWithFrame吗?我不清楚为什么要删除和添加子视图。为什么不在自定义单元格的init方法(可能是initWithCoder:)中添加子视图,然后使用它来完成?在init方法中调用prepareForReuse也没有意义,因为此时单元格是全新的。您使用的是自定义布局还是
UICollectionViewFlowLayout
?两者都使用。它是UiCollectionViewFlowLayout的一个子类。也许我不理解dequeueReusableCellWithReuseIdentifier方法的用途。它要么调用initWithFrame,要么调用prepareForReuse,对吗?它找到UICollectionViewCell对象并返回它。该单元格的状态可能会有指针指向在单元格滚动出视图之前设置的任何子视图,因此需要将其重置为与当前使用的单元格的模型相匹配的图像。我希望init和prepforeuse做同样的事情。是,正在调用initWithFrame。
-(void)didLoadBuilderCellView:(NSNotification*)note{
BuilderCellView* cellView = [[note userInfo] objectForKey:@"cell"];
BuilderSlideView* slideView = (BuilderSlideView*)cellView;
NSIndexPath* indexPath = [self indexPathForSlide:slideView.slide];
if(indexPath)
    [self.collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];}