如何在tabelview Cell中单击Collectionview时查找tableview的索引路径

时间:2017-06-06 05:55:51

标签: ios objective-c uitableview uicollectionview

我有一个tableview单元格,里面有集合视图。

CollectionView包含来自服务器的图像。

我想要用户点击的特定图像。 即 让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];

}

2 个答案:

答案 0 :(得分:3)

示例TableView代码:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CellTableTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellCol"];
// Give Tag to CollectionView
    cell.collectionView.tag = indexPath.row;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",(long)indexPath.row);

}

示例ColleCtionView代码:

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *celll = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    celll.contentView.backgroundColor = [UIColor greenColor];
    return celll;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",(long)collectionView.tag);
}

答案 1 :(得分:0)

设置UITableView时,将标签设置为UICollectionView。

as

collectionView.tag = indexPath.row

并在collectionViewDidSelect委托方法中检查collectionView的标签