- [__ NSCFArray objectAtIndex:]:索引(2)超出界限(2)'
***首先抛出调用堆栈:
#pragma mark - Collection view data source
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return totalCountQuestion;
}
-(UICollectionViewCell *)collectionView:(UICollectionView*)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath{
QuestionCell *Quizcell=(QuestionCell *)[cv dequeueReusableCellWithReuseIdentifier:@"QuizCell" forIndexPath:indexPath];
Quizcell.TotalQuestions.text=[NSString stringWithFormat:@"%ld",(long)indexPath.item+1];
Question *question = self.currentQuiz[indexPath.row];
Quizcell.image_Crt_Wrg.image=(question.selectedResponse == question.correctResponse) ? [UIImage imageNamed:@"Correct_images.png"] : [UIImage imageNamed:@"WrongShape.png"];
return Quizcell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
index_value=indexPath.row;
}
//| -------------------------------------------------------------------------
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
Quiz *newQuiz = [[Quiz alloc] initWithQuestionsPlistAtURL:[[NSBundle mainBundle] URLForResource:TopicListArray[TopicIndex] withExtension:@"plist"]];
NSIndexPath *selectedIndexPath=[[self.collectionView indexPathsForSelectedItems]objectAtIndex:index_value];
_selectedCell=selectedIndexPath.item;
ExplanationViewController *FirstQuestionVC = (ExplanationViewController*)[segue.destinationViewController viewControllers][0];
FirstQuestionVC.currentQuiz=newQuiz;
FirstQuestionVC.selectedCell=_selectedCell;
}
任何人帮助我,..
答案 0 :(得分:0)
首先:我会用
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
而不是
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
但我的猜测是你的问题在
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
//...
NSIndexPath *selectedIndexPath=[[self.collectionView indexPathsForSelectedItems]objectAtIndex:index_value];
_selectedCell=selectedIndexPath.item;
我猜我只选择了一个项目,所以你应该使用
NSIndexPath *selectedIndexPath = [[self.collectionView indexPathsForSelectedItems] firstObject];
和 NSIndexPath 不提供属性项。
我会在 collectionView:didSelectItemAtIndexPath 中实现 pushViewController:animated:。对于你可能 performSegueWithIdentifier:sender:是一个选项。