简体   繁体   中英

How to find the indexpath of tableview when Collectionview is clicked which is in tabelview Cell

I have tableview cell which is having collection view in it.

CollectionView contains images coming from server.

I want particular image on which user has tapped. ie Let Tableview have 4 rows. In the four row I have collection in every tableview row. When I click on 3rd row collectionview cell, I have to get that image from that row.

code is here

    - (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];

}

Sample TableView Code:

-(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);

}

Sample ColleCtionView Code:

-(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);
}

While setting up UITableView set a tag to UICollectionView.

as

collectionView.tag = indexPath.row

And in collectionViewDidSelect delegate method check tag of the collectionView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM