0

我是一名新的 iOS 开发者;我有一个带有解析和动态单元格的应用程序,但是当我运行该应用程序时,我发现该应用程序由于标题上的原因而崩溃,我的代码如下

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    
    myArray = [[NSMutableArray alloc] init];
    
    
    //create new view if no view is available for recycling
    
   // view = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 250.0f, 300.0f)] autorelease];
    
    FXImageView *imageView = [[[FXImageView alloc] initWithFrame:CGRectMake(0, 0, 250.0f, 350.0f)] autorelease];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.asynchronous = YES;
   // imageView.reflectionScale = 0.5f;
   // imageView.reflectionAlpha = 0.25f;
  //  imageView.reflectionGap = 10.0f;
    imageView.shadowOffset = CGSizeMake(0.0f, 2.0f);
    imageView.shadowBlur = 5.0f;
    imageView.cornerRadius = 10.0f;
    view = imageView;
    
    [ProgressHUD dismiss];
    
     NSString *string1 = [[NSUserDefaults standardUserDefaults] stringForKey:@"Class"];
    
    PFQuery *query = [PFQuery queryWithClassName:[NSString stringWithFormat:@"%@",string1]];
    
    
    query.cachePolicy = kPFCachePolicyNetworkElseCache;
    
    //show loader view
    
    
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            
            myArray = [[NSMutableArray alloc] initWithArray:objects];
            
            PFObject *object = [myArray objectAtIndex:index];
            
            
            [file getDataInBackgroundWithBlock:^(NSData *data1, NSError *error) {
                if (!error) {
                    
                    ((UIImageView *)view).image = [UIImage imageWithData:data1];
                    
                    //[HUD hideUIBlockingIndicator];
                    
                }
            }];
            
        }
        
    }];

    return view;

    
}

我将第一个屏幕 UICollectionView 用于解析为以下代码的动态数据

pragma collectionView 委托

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
    CALayer *layer = cell.layer;
    [layer setCornerRadius:8.0f];
    [layer setMasksToBounds:YES];
   // [layer setBorderWidth:1.0f];
  //  layer.backgroundColor = [UIColor whiteColor].CGColor;
    //can you click
    
    PFObject *imageObject = [myArray objectAtIndex:indexPath.item];
    PFFile *imageFile = [imageObject objectForKey:@"image"];
    NSString *name = [imageObject objectForKey:@"name"];
    
    [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (!error) {
            
            UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
            imageView.image = [UIImage imageWithData:data];
            
            UILabel *title2 = (UILabel *)[cell viewWithTag:200];
            title2.text = [NSString stringWithFormat:@"%@",name];
            title2.font = [UIFont fontWithName:@"GESSTextMedium-Medium" size:12];
        }
    }];

    
    return cell;
}

我需要一些帮助才能知道错误在哪里;每次我在控制台上收到此错误[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]

4

6 回答 6

7

没有足够的信息。我们甚至不知道这次崩溃的确切路线。中的实际对象数量是myArray多少?你返回什么collectionView:numberOfItemsInSection: 等等..

恕我直言,最简单的解决方案是调试此崩溃。并找出为什么索引比objectAtIndex:预期的大。尝试这个:

1)打开“断点导航器”

2)点击“+”按钮并选择“添加异常断点”选项

现在应用程序将在调试时对任何异常使用此断点。

于 2014-10-22T22:06:26.373 回答
1

问候和欢迎,新开发人员。您要找出的第一件事是发生异常的位置。为此,您需要打开“异常断点”。

您执行此操作的确切方式因 Xcode 版本而异(感谢 Apple)。假设您使用的是 Xcode 6,这里有一个视频来解释它。

我认为您的轮播很可能漏报了它拥有的项目数量。或者由于某些其他原因,您正试图获取一个不存在的轮播项目……也许轮播nil在使用现有范围之外的单元格索引进行查询时需要 a,但您假设索引有效并命中数组边界异常。在一个疯狂的猜测中,错误可能与这里的行有关:

…
myArray = [[NSMutableArray alloc] initWithArray:objects];
PFObject *object = [myArray objectAtIndex:index];
…

我猜这section不是 to 的有效索引myArray

(顺便说一下 -myArrayobjects数组的可变副本。你为什么这样做?你没有对 进行任何更改myArray,所以看起来没有必要。)


您的集合视图委托中可能有一个方法collectionView: numberOfItemsInSection:。或者,如果你不这样做,你可能需要一个。此刻的代码是什么?

于 2014-10-22T21:57:26.517 回答
0

您的问题出在这一行:PFObject *object = [myArray objectAtIndex:index];

您的索引为 2,但您的数组中只有 2 个对象。由于数组的最大索引是 1 (0..1),它会崩溃。我建议查看调用该方法的位置以及如何获得超出范围的索引。

于 2014-10-22T21:47:44.707 回答
0

它可能是这两行之一:

PFObject *object = [myArray objectAtIndex:index];

或者

PFObject *imageObject = [myArray objectAtIndex:indexPath.item];

问题是 index(或第二种情况下的 indexPath.item)包含值 2,但 myArray 的大小为 2(因此唯一有效的索引是 0 和 1)。

于 2014-10-22T21:47:52.080 回答
0

请检查您正在使用的 numberOfRowsInSection 方法和数组计数。

于 2015-04-26T11:55:11.390 回答
0

最后我找到了问题所在;我必须为 iCarousel numberOfItems 定义数组,因此代码如下

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    //return the total number of items in the carousel

    return ([myArray count]);
    return ([titleArray count]);
    return ([priceArray count]);
    return ([sapArray count]);    
}

所以我摆脱了索引问题

感谢所有与我分享

于 2014-10-22T22:38:47.800 回答