我的代码中有一点问题,我使用了带有segmentedControl的CollectionView,但是当我按下segmetedControl的第2或第3部分并滚动CollectionView时,我收到此错误
*由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* - [__ NSArrayI objectAtIndex:]:索引16超出边界[0 .. 15]'
#define API_V3_CHANNEL_URL @"examples.json"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self fetchEntries];
[self fetchEntriesWinner];
[self fetchEntriesPhotos];
}
- (void)fetchEntries
{
NSString *searchURL = [API_V3_CHANNEL_URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *searchData = [NSData dataWithContentsOfURL:[NSURL URLWithString:searchURL]];
NSDictionary *searchDict =[NSJSONSerialization JSONObjectWithData:searchData options:NSJSONReadingMutableContainers error:nil];
//self.readArray = [searchDict objectForKey:@"CONTESTS"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status == %@", @"LIVE"];
self.readArray = [[searchDict objectForKey:@"CONTESTS"] filteredArrayUsingPredicate:predicate];
NSLog(@"%@", self.readArray);
}
- (void)fetchEntriesWinner
{
NSString *searchURL = [API_V3_CHANNEL_URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *searchData = [NSData dataWithContentsOfURL:[NSURL URLWithString:searchURL]];
NSDictionary *searchDict =[NSJSONSerialization JSONObjectWithData:searchData options:NSJSONReadingMutableContainers error:nil];
//self.readArray = [searchDict objectForKey:@"CONTESTS"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status == %@", @"ARCHIVED"];
self.readArrayWinner = [[searchDict objectForKey:@"CONTESTS"] filteredArrayUsingPredicate:predicate];
NSLog(@"winner array : %@", self.readArrayWinner);
}
- (void)fetchEntriesPhotos
{
NSString *searchURL = [API_V3_CHANNEL_URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *searchData = [NSData dataWithContentsOfURL:[NSURL URLWithString:searchURL]];
NSDictionary *searchDict =[NSJSONSerialization JSONObjectWithData:searchData options:NSJSONReadingMutableContainers error:nil];
//self.readArray = [searchDict objectForKey:@"CONTESTS"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status == %@", @"LIVE"];
self.readArrayPhotos = [[searchDict objectForKey:@"CONTESTS"] filteredArrayUsingPredicate:predicate];
NSLog(@"%@", self.readArray);
}
- (IBAction)segmentedControlAction:(id)sender
{
[self.collectionView reloadData];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
switch (_segmentedControl.selectedSegmentIndex) {
case 0:
return [self.readArray count];
break;
case 1:
return [self.readArrayWinner count];
break;
case 2:
return [self.readArrayPhotos count];
break;
default:
break;
}
return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"pictureCell";
MSContestListCollectionViewCell *cell = (MSContestListCollectionViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *searchResult = [self.readArray objectAtIndex:indexPath.item];
NSDictionary *searchResult2 = [self.readArrayWinner objectAtIndex:indexPath.item];
NSDictionary *searchResult3 = [self.readArrayPhotos objectAtIndex:indexPath.item];
cell.titleContest.adjustsFontSizeToFitWidth = YES;
cell.titleContest.minimumScaleFactor = 0.5;
cell.pictureImageView.layer.cornerRadius = 5;
cell.pictureImageView.clipsToBounds = YES;
cell.titleView.layer.cornerRadius = 5;
cell.titleView.clipsToBounds = YES;
switch (_segmentedControl.selectedSegmentIndex) {
case 0: {
NSString *stringImage = [searchResult objectForKey:@"featuredImage"];
NSString *image = [NSString stringWithFormat:@"https://srv.mediaswapp.com/%@", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
cell.titleContest.text = [searchResult objectForKey:@"description"];
cell.statusContest.text = [searchResult objectForKey:@"status"];
break;
}
case 1: {
NSString *stringImage = [searchResult2 objectForKey:@"featuredImage"];
NSString *image = [NSString stringWithFormat:@"https://srv.mediaswapp.com/%@", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
cell.titleContest.text = [searchResult2 objectForKey:@"description"];
cell.statusContest.text = [searchResult2 objectForKey:@"status"];
break;
}
case 2: {
NSString *stringImage = [searchResult3 objectForKey:@"featuredImage"];
NSString *image = [NSString stringWithFormat:@"https://srv.mediaswapp.com/%@", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
cell.titleContest.text = [searchResult3 objectForKey:@"description"];
cell.statusContest.text = [searchResult3 objectForKey:@"status"];
break;
}
default:
break;
}
return cell;
}
@end
答案 0 :(得分:2)
您必须在开关盒内移动以下三行:
NSDictionary *searchResult = [self.readArray objectAtIndex:indexPath.item];
NSDictionary *searchResult2 = [self.readArrayWinner objectAtIndex:indexPath.item];
NSDictionary *searchResult3 = [self.readArrayPhotos objectAtIndex:indexPath.item];
应该是
switch (_segmentedControl.selectedSegmentIndex) {
case 0: {
NSDictionary *searchResult = [self.readArray objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult objectForKey:@"featuredImage"];
NSString *image = [NSString stringWithFormat:@"https://srv.mediaswapp.com/%@", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
cell.titleContest.text = [searchResult objectForKey:@"description"];
cell.statusContest.text = [searchResult objectForKey:@"status"];
break;
}
case 1: {
NSDictionary *searchResult2 = [self.readArrayWinner objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult2 objectForKey:@"featuredImage"];
NSString *image = [NSString stringWithFormat:@"https://srv.mediaswapp.com/%@", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
cell.titleContest.text = [searchResult2 objectForKey:@"description"];
cell.statusContest.text = [searchResult2 objectForKey:@"status"];
break;
}
case 2: {
NSDictionary *searchResult3 = [self.readArrayPhotos objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult3 objectForKey:@"featuredImage"];
NSString *image = [NSString stringWithFormat:@"https://srv.mediaswapp.com/%@", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
cell.titleContest.text = [searchResult3 objectForKey:@"description"];
cell.statusContest.text = [searchResult3 objectForKey:@"status"];
break;
}
default:
break;
}
原因是当readArray
包含16个元素并且您尝试显示readArray
的部分时,您还将尝试访问索引16处的readArrayWinner
,这可能不会出现数组较小。
答案 1 :(得分:0)
数组包含16个对象,您将找到objectAtIndex“16”,这意味着您在数组中找到第17个不可用的对象,因此使用未捕获的异常'NSRangeException'终止应用程序。