我正在使用集合视图,我有6个单元格在集体视图中,我想单元格单击时呈现不同的网址,我该怎么做?在一系列网址的帮助下?还是有另一种方法来做到这一点?web视图不加载url
FirstViewController.m
-(void)viewDidLoad{
array=[NSArray arrayWithObjects:@"home.jpg",@"about-us.png",@"aarti.png",@"video.png",@"news.png",@"contact.png", nil];
NSString *[email protected]"url1";
NSString *[email protected]"url2";
NSString *[email protected]"url3";
NSString *[email protected]"url4";
NSString *[email protected]"url5";
NSString *[email protected]"url6";
urls=[NSArray arrayWithObjects:url1,url2,url3,url4,url5,url6, nil];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return array.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"collect";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[array objectAtIndex:indexPath.row]];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSString *urlAddress = [urls objectAtIndex:indexPath.row];//url at clicked index from array
NSURL *url = [NSURL URLWithString:urlAddress];
WebViewController *webView=[[WebViewController alloc]init];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView.webview loadRequest:requestObj];
}
读任何MVC结构或教程的CollectionView – Shubhank
@Shubhank是的,我有形成具有6个细胞的集合视图,现在我想提出关于Web视图6个网址,我已经创建的Web视图并成功呈现和URL但我不知道如何呈现不同的网址,尊重单元格的索引。 –
这里有一个代表'didSelectItemAtIndexPath'的集合方法,您可以在这里获取位置的url并在webview中显示它,或者通过segue传递任何您想要的内容。 – Shubhank