0

我正在使用网络服务来获取我的应用程序中的数据。正如你在这里看到的,我得到了不同的壁纸图像。

"wallpapers": [

    {
        "url": "http://www.krcgenk.be/files/wallpaper/app/iphone4_fans.jpg"
    },
    {
        "url": "http://www.krcgenk.be/files/wallpaper/app/iphone4_logo.jpg"
    },
    {
        "url": "http://www.krcgenk.be/files/wallpaper/app/iphone4_simaeys.jpg"
    },
    {
        "url": "http://www.krcgenk.be/files/wallpaper/app/iphone4_trainers.jpg"
    },
    {
        "url": "http://www.krcgenk.be/files/wallpaper/app/iphone4_vossen.jpg"
    }

]

如您所见,目前壁纸的数量是奇数。所以表格视图中的一个单元格只包含 1 个图像而不是 2 个。我尝试执行以下操作。

id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:0];
        NSInteger count = [sectionInfo numberOfObjects];

        if (_loopIndex < count)
        {
           NSIndexPath *path = [NSIndexPath indexPathForRow:_loopIndex inSection:0];
            _loopIndex++;
            NSIndexPath *path2 = [NSIndexPath indexPathForRow:_loopIndex inSection:0];
            Wallpaper *wallpaper1 = [self.fetchedResultsController objectAtIndexPath:path];
            Wallpaper *wallpaper2 = [self.fetchedResultsController objectAtIndexPath:path2];

            NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:wallpaper1.url]];
            UIImage* image = [[UIImage alloc] initWithData:imageData];
            NSData* imageData2 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:wallpaper2.url]];
            UIImage* image2 = [[UIImage alloc] initWithData:imageData2];

           if(!(image == nil)){
                cell.img1.image = image;
           }else{
               NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.krcgenk.be/files/wallpaper/app/iphone4_logo.jpg"]];
               UIImage* image = [[UIImage alloc] initWithData:imageData];
               cell.img1.image = image;
           }
            if(!(image2 == nil)){
               cell.img2.image = image2; 
            }else{
                NSData* imageData2 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.krcgenk.be/files/wallpaper/app/iphone4_logo.jpg"]];
                UIImage* image2 = [[UIImage alloc] initWithData:imageData2];
                cell.img2.image = image2;
            }
        }
        _loopIndex++;
        if(_loopIndex >= count){
            _loopIndex = count-1;
        }

        return cell;

它在我的 imageView 中正确填充了前四个图像,但在第五个图像上崩溃了​​。这是错误。

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFArray objectAtIndex:]: index (5) beyond bounds (5)

提前致以诚挚的问候和感谢

编辑

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
        id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];

        NSInteger count = [sectionInfo numberOfObjects];
        return count;

}
4

1 回答 1

0

您的表格视图需要 6 行。但您的数据源数组仅包含 5 个项目。检查- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView表视图的委托方法。

于 2012-10-19T08:25:16.030 回答