【问题标题】:How to find the indexpath of tableview when Collectionview is clicked which is in tabelview Cell单击tableview单元中的Collectionview时如何找到tableview的索引路径
【发布时间】: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


【解决方案1】:

示例表视图代码:

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

}

示例ColleCtion查看代码:

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

【讨论】:

  • 您好,vishal,感谢您的回答。Did 也提供了标签,但在 didSelectItemAtIndexPath 中显示了 collectionView.tag=last tableviewindex
【解决方案2】:

在设置 UITableView 时,为 UICollectionView 设置一个标签。

作为

collectionView.tag = indexPath.row

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

【讨论】:

  • 当我在 collectionviewdidselect 中记录它时,我在 tableview cellforrowatindexpath 中为集合提供标签,它显示 collectionview.tag=lastindex of tabelviewcell
  • 看不懂,tabelviewcell的lastindex是什么意思
  • 如果您在 tableview 中有 4 行,在 tabelviewcell 中为 indexpath 的行 cell.imgcollection_view.tag=indexPath.row;现在我正在打印 cell.imgcollection_view 标签和用户点击 collectioncell 时的结果在 tableview 的 3 行中,但我得到的结果是 cell.imgcollection_view tag=3 ,但应该是 2
猜你喜欢
  • 1970-01-01
  • 2019-04-25
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-02
相关资源
最近更新 更多