我正在使用网络服务来获取我的应用程序中的数据。我在这里可以看到不同的壁纸图像。
"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"
}
]
正如你所看到的,目前壁纸的数量是一个奇数。所以tableview中的一个单元格只包含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;
}
答案 0 :(得分:0)
您的表视图需要6行。但是您的数据源数组只包含5个项目。检查表视图的- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
委托方法。