NSMutableArray insertObject:atIndex导致崩溃

时间:2012-07-23 22:42:53

标签: ios ipad nsmutablearray

我正在为iPad制作幻灯片创建应用程序,当我让用户重新安排幻灯片时遇到一些麻烦。当调用slideDropped方法时,如果我只是删除幻灯片然后将其添加到幻灯片放映的末尾,那么一切正常。如果我说5幻灯片幻灯片的幻灯片2并将其移动到幻灯片3然后当我尝试循环slideshow.slides(这只是一个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];
}

关于我可能做错什么的任何想法?我怀疑我没有正确管理幻灯片,但我不确定如何做到这一点。

1 个答案:

答案 0 :(得分:2)

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