【发布时间】:2017-06-06 05:55:50
【问题描述】:
我有一个表格视图单元格,其中包含集合视图。
CollectionView 包含来自服务器的图像。
我想要用户点击过的特定图像。 IE。 让 Tableview 有 4 行。在四行中,我在每个 tableview 行中都有集合。当我单击第 3 行 collectionview 单元格时,我必须从该行获取该图像。
代码在这里
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell = (ISclassifiedCell*)[_isclassifed_tblview dequeueReusableCellWithIdentifier:@"ISclassifiedCell" forIndexPath:indexPath];
[cell.layer setCornerRadius:4.0f];
[cell.layer setMasksToBounds:YES];
cell.profile_img.layer.cornerRadius=4.0f;
cell.profile_img.layer.masksToBounds = YES;
cell.iseventtype_lbl.text=[[isclassifiedarray objectAtIndex:indexPath.section]valueForKey:@"title"];
cell.eventtype_lbl.text=[[isclassifiedarray objectAtIndex:indexPath.section]valueForKey:@"category"];
cell.description_lbl.text=[[isclassifiedarray objectAtIndex:indexPath.section]valueForKey:@"description"];
UIFont *font=[UIFont fontWithName:@"Montserrat" size:14.0];
CGFloat size = [self getLabelHeightForString:cell.description_lbl.text font:font];
cell.description_lbl.frame=CGRectMake(cell.description_lbl.frame.origin.x, cell.description_lbl.frame.origin.y, cell.description_lbl.frame.size.width, size);
NSString *clubberid=[NSString stringWithFormat:@"%@",[[isclassifiedarray objectAtIndex:indexPath.section]valueForKey:@"clubberId"]];
cell.clubbername_lbl.text=[[isclassifiedarray objectAtIndex:indexPath.section]valueForKey:@"clubberName"];
if ([clubberid isEqualToString:[NSString stringWithFormat:@"%@",mainclubberId]]) {
[cell.editbutnoutlet setImage:[UIImage imageNamed:@"note-interface-symbol"] forState:UIControlStateNormal];
cell.pokebtnoutlet.hidden=YES;
cell.editbutnoutlet.hidden=NO;
cell.editbutnoutlet.tag = indexPath.section;
[cell.editbutnoutlet addTarget:self action:@selector(editButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[_providertbl reloadData];
}else{
[cell.pokebtnoutlet setImage:[UIImage imageNamed:@"hold"] forState:UIControlStateNormal];
cell.pokebtnoutlet.hidden=NO;
cell.editbutnoutlet.hidden=YES;
cell.pokebtnoutlet.tag = indexPath.section;
[cell.pokebtnoutlet addTarget:self action:@selector(pokeButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
NSString *imgUrl = [NSString stringWithFormat:@"%s/presignin/clubber/getImage?clubberId=%@",urlPath,clubberid];
NSURL *imageURL=[NSURL URLWithString:imgUrl];
imgarray=[[isclassifiedarray objectAtIndex:indexPath.section]valueForKey:@"media"];
cell.profile_img.imageURL=imageURL;
cell.imgcollection_view.tag=indexPath.section;
if (imgarray.count==0) {
cell.imgcollection_view.hidden=YES;
}else{
cell.imgcollection_view.hidden=NO;
}
cell.imgcollection_view.delegate=self;
cell.imgcollection_view.dataSource=self;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//cell.imgcollection_view.allowsSelection=NO;
return cell;
}
}
collectionviewcode
- (NSInteger)collectionView:(UICollectionView *)view
numberOfItemsInSection:(NSInteger)section {
return imgarray.count;
}
// 2
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView
*)collectionView {
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//isclasifiedimageCell *cell1 = nil;
cell1=[cv
dequeueReusableCellWithReuseIdentifier:@"isclasifiedimageCell"
forIndexPath:indexPath];
NSLog(@"%ld",(long)indexPath.row);
NSString *clubberid=[NSString stringWithFormat:@"%@",[[imgarray objectAtIndex:indexPath.row]valueForKey:@"idclassifieds_media"]];
NSString *clubberidtype=[NSString stringWithFormat:@"%@",[[imgarray objectAtIndex:indexPath.row]valueForKey:@"type"]];
}
else{
NSString *typeimgUrl1 = [NSString stringWithFormat:@"%s/presignin/classifieds/showMedia?idclassifieds_media=%@",urlPath,clubberid];
NSURL *imageURL=[NSURL URLWithString:typeimgUrl1];
cell1.img_view.imageURL=imageURL;
}
return cell1;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%ld",(long)cell.imgcollection_view.tag);
mediaimgarray=[[isclassifiedarray objectAtIndex:cell.imgcollection_view.tag]valueForKey:@"media"];
NSString *cluderimgid=[NSString stringWithFormat:@"%@",[[mediaimgarray objectAtIndex:indexPath.row]valueForKey:@"idclassifieds_media"]];
// NSString *mediatypeurlstr=[NSString stringWithFormat:@"%@",[[mediaimgarray objectAtIndex:indexPath.row]valueForKey:@"idclassifieds_media"]];
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main"
bundle:nil];
isclasifiedImgpreviewVC * isclasifiedImgpreview =
[storyboard instantiateViewControllerWithIdentifier:@"isclasifiedImgpreview"];
// isclasifiedImgpreview.mediaatype=cluderimgid;
isclasifiedImgpreview.mediaatypeurlid=cluderimgid;
[self presentViewController:isclasifiedImgpreview
animated:YES
completion:nil];
}
【问题讨论】:
-
@Satheeshkumar Naidu 使用集合视图 DidSelect 方法。
-
你必须展示你是如何在 tableview 中添加集合视图以及你想在哪里获取图像的。即 Viewcontroller 或集合视图委托?
-
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { cell1=[cv dequeueReusableCellWithReuseIdentifier:@"isclasifiedimageCell" forIndexPath:indexPath]; NSLog(@"%ld",(long)indexPath.row); NSString *clubberid=[NSString stringWithFormat:@"%@",[[imgarray objectAtIndex:indexPath.row]valueForKey:@"idclassifieds_media"]]; }else{ NSURL *imageURL=[NSURL URLWithString:typeimgUrl1]; cell1.img_view.imageURL=imageURL;返回单元格1; }
-
编辑您的答案并向我们展示您的代码
标签: ios objective-c uitableview uicollectionview