0

我正在为 iPad 开发幻灯片创建应用程序,当我让用户重新排列幻灯片时遇到了一些麻烦。当调用 slideDropped 方法时,如果我只是删除幻灯片然后将其添加到幻灯片的末尾,那么一切正常。如果我说 5 张幻灯片的幻灯片 2 并将其移动到幻灯片 3,那么当我尝试循环播放slideshow.slides(这只是 a NSMutableArray)时,它会在新幻灯片上出现错误的访问错误而崩溃。

-(void)slideDropped:(Slide *)slide
{
    int destIndex = (int)(slide.frame.origin.x) / 152;
    destIndex = MIN(destIndex, slideshow.slides.count - 1);
    destIndex = MAX(0, destIndex);
    int sourceIndex = [slideshow.slides indexOfObject:slide];
    [slideshow removeSlideAtIndex:sourceIndex];
    if(destIndex >= slideshow.slides.count)
        [slideshow addSlide:slide];
    else
        [slideshow insertSlide:slide atIndex:destIndex];
    [self arrangeSlides];
}

-(void)arrangeSlides
{
    [scrSlideshow.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    int x = 0;
    NSLog(@"There are %i slides",slideshow.slides.count);
    for(Slide* slide in slideshow.slides)
    {
        NSLog(@"%@",slide);//crashes here on the new slide if I insert at a specific index rather than appending the slide to the end
        CGRect frame = CGRectMake(x++ * 152, 21, 150, 150);
        slide.frame = frame;
        [scrSlideshow addSubview:slide];
    }
    [scrSlideshow setContentSize:CGSizeMake(slideshow.slides.count * 152, 150)];
}

//in the Slideshow class

-(void)addSlide:(Slide *)slide
{
    [slides addObject:slide];
}

-(void)removeSlideAtIndex:(int)index
{
    [slides removeObjectAtIndex:index];
}

-(void)insertSlide:(Slide *)slide atIndex:(int)index
{
    [slides insertObject:slides atIndex:index];
}

关于我可能做错了什么的任何想法?我怀疑我没有正确管理幻灯片,但我不知道该怎么做。

4

1 回答 1

2

查看该代码的底部。你有insertObject:slides. 这应该只是“幻灯片”还是您打算让它成为复数?那可能是你的问题

于 2012-07-23T22:48:06.913 回答