当我在第一个视图中点击tableview任何单元格时,如何在第二个视图中显示collectionview

时间:2015-02-02 07:23:03

标签: ios objective-c

我正在显示tableview,当我点击tableview任何单元格我想显示带有images的集合视图。但是在这里我得到第二个视图中的空白视图不在集合视图中显示图像。 我的代码是这样的

viewDidLoad{
freshArray=[NSMutableArray arrayWithObjects:[UIImage imageNamed:@"1.png"],     [UIImage imageNamed:@"2.png"],[UIImage imageNamed:@"3.png"],[UIImage  imageNamed:@"4.png"],[UIImage imageNamed:@"5.png"],[UIImage imageNamed:@"6.png"],[UIImage imageNamed:@"7.png"],[UIImage imageNamed:@"8.png"],[UIImage imageNamed:@"9.png"],nil];

popularArray=[NSMutableArray arrayWithObjects:[UIImage imageNamed:@"10.png"],[UIImage imageNamed:@"11.png"],[UIImage imageNamed:@"12.png"],[UIImage imageNamed:@"13.png"],[UIImage imageNamed:@"14.png"],[UIImage imageNamed:@"15.png"],[UIImage imageNamed:@"16.png"],[UIImage imageNamed:@"17.png"],[UIImage imageNamed:@"18.png"],nil];

AdviceArray=[NSMutableArray arrayWithObjects:[UIImage imageNamed:@"19.png"],[UIImage imageNamed:@"20.png"],[UIImage imageNamed:@"21.png"],[UIImage imageNamed:@"22.png"],[UIImage imageNamed:@"23.png"],[UIImage imageNamed:@"24.png"],[UIImage imageNamed:@"25.png"],[UIImage imageNamed:@"26.png"],[UIImage imageNamed:@"27.png"],nil];
AllitemsArray=[[NSMutableArray  alloc]initWithObjects:freshArray,popularArray,AdviceArray,AwesomeArray,CartoonArray, nil];
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
secondViewController *second=[[secondViewController   alloc]initWithNibName:@"secondViewController" bundle:nil];   
second.recipeImageView.image=[AllitemsArray objectAtIndex:indexPath.row];
second.secCollectionImagesArray=AllitemsArray;
[self.navigationController pushViewController:second animated:YES];
}
secondViewController.m
- (NSInteger)collectionView:(UICollectionView *)collectionView   numberOfItemsInSection:(NSInteger)section
{
return [secCollectionImagesArray count];

}
- (MyCell *)collectionView:(UICollectionView *)collectionView  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL"  forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
UILabel *descLabel = (UILabel *)[cell viewWithTag:110];

[descLabel setText:[secCollectionImagesArray objectAtIndex:indexPath.row]];
recipeImageView.image = [UIImage imageNamed:[secCollectionImagesArray  objectAtIndex:indexPath.row]];

int pages = floor(collectionView.contentSize.width / collectionView.frame.size.width) + 1;
[pageControl setNumberOfPages:pages];

return cell;
}
any help Appreciated

1 个答案:

答案 0 :(得分:0)

你应该尝试这样

UIImageView * recipeImageView =(UIImageView *)[cell.contentView viewWithTag:100]; UILabel * descLabel =(UILabel *)[cell.contentView viewWithTag:110];

或者 看起来你已经将collectionviewcell子类化了,然后你可以像这样访问UIImageView和uilabel cell.label = @ “文本”;