首页 > 解决方案 > 将collectionview添加到uitableviewcell并为tableview的特定单元重新加载数据

问题描述

如何使用隐藏和取消隐藏功能将来自服务器的数据绑定到位于 uitableviewcell 内的 collectionview。点击 uibutton 我想让 colectionview 可见并在点击按钮时再次隐藏它。当我滚动 tableview 内的数据时会发生什么集合视图从一个单元格跳到另一个单元格,使集合视图为空白。

-(void)arrowButtonTapped:(UIButton *)donwbtn{

myob = [NSString stringWithFormat:@"%li", (long)donwbtn.tag];
NSLog(@"%@",myob);

NSIndexPath *  indexPath = [NSIndexPath indexPathForRow:0 
inSection:donwbtn.tag];

OppCell* celll = [_tableView cellForRowAtIndexPath:indexPath];
celll.collView.tag=donwbtn.tag;



if ([checkcoll containsObject:myob]) {
    [checkcoll removeObject:myob];
    celll.collView.hidden  = true;
    celll.collvwhght.constant=0;
    celll.colltop.constant=0;
    celll.collbottom.constant=0;
}

else
{
    [checkcoll addObject:myob];
    celll.collView.hidden = false;
    celll.collvwhght.constant=229;
    celll.colltop.constant=9;
    celll.collbottom.constant=8;
    [celll.collView reloadData] ;

  }


  [self.tableView beginUpdates];

   [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

  [self.tableView endUpdates];

  }

    -(UITableViewCell *)tableView:(UITableView *)tableView 
  cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   OppCell *cell = (OppCell *)[tableView 
    dequeueReusableCellWithIdentifier:@"OppCell"];

   NSString *theIndexpath = [NSString stringWithFormat:@"%ld", 
   (long)indexPath.section];

   if ([checkcoll containsObject:theIndexpath])
  {
    cell.collView.hidden = false;
    cell.collvwhght.constant=229;
    cell.colltop.constant=9;
    cell.collbottom.constant=8;


      if((![[[_Activitylistarray 

      objectAtIndex:indexPath.section]
      valueForKey:@"OpportunityContactInfoList"] 
     isKindOfClass:[NSNull class]])&&([[[_Activitylistarray 
     objectAtIndex:indexPath.section]
    valueForKey:@"OpportunityContactInfoList"]count]>0)){



        if(cell.contactdetailsarray == nil) {
            cell.contactdetailsarray =[[NSMutableArray alloc]init];
        for(NSDictionary * contactdict in 
      [[_Activitylistarray objectAtIndex:indexPath.section]
     valueForKey:@"OpportunityContactInfoList"] ){

          [cell.contactdetailsarray addObject:contactdict];
            [cell.collView reloadData];
        }

        }




      }

     }
   else
  {
    cell.collView.hidden  = true;
    cell.collvwhght.constant=0;
    cell.colltop.constant=0;
    cell.collbottom.constant=0;

  //  [cell.contactdetailsarray removeAllObjects];
   // [cell.collView reloadData];
  }

   ////Code in UITableViewCell

     #import "OppCell.h"
    #import "OppsContactCell.h"

   @implementation OppCell

    - (void)awakeFromNib {
    [super awakeFromNib];
   // Initialization code

  [self.collView registerNib:[UINib nibWithNibName:@"OppsContactCell" 
     bundle:nil] forCellWithReuseIdentifier:@"OppsContactCell"];
     self.collView.pagingEnabled = YES;

     UICollectionViewFlowLayout * layout=[[UICollectionViewFlowLayout 
     alloc]init];

  layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
    self.collView.collectionViewLayout = layout;
      self.collView.decelerationRate = 
            UIScrollViewDecelerationRateNormal;

      //  _contactdetailsarray=[NSMutableArray array];

      // self.collvwhght.constant=0;
       //self.collView.hidden=YES;
        }

   - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
     [super setSelected:selected animated:animated];

     // Configure the view for the selected state
        }






     - (UICollectionViewCell *)collectionView:(UICollectionView 
   *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

     OppsContactCell *cell = [collectionView 
      dequeueReusableCellWithReuseIdentifier:@"OppsContactCell" 
      forIndexPath:indexPath];


      cell.contactimgvw.image = [cell.contactimgvw.image 
     imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
      [cell.contactimgvw setTintColor:[UIColor blackColor]];

         cell.numberimgvw.image = [cell.numberimgvw.image 
        imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
       [cell.numberimgvw setTintColor:[UIColor blackColor]];

        cell.emailimgvw.image = [cell.emailimgvw.image 
     imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
     [cell.emailimgvw setTintColor:[UIColor blackColor]];



        if(_contactdetailsarray.count==1){



            if(([[[_contactdetailsarray 
     objectAtIndex:0]valueForKey:@"Designation"]isEqualToString:@""])&&([[[_contactdetailsarray 
objectAtIndex:0]valueForKey:@"Department"]isEqualToString:@"" ])){

        cell.lbldesignation.text=@"";

        }

       else{

        cell.lbldesignation.text=[NSString stringWithFormat:@"%@,%@", 
   [[_contactdetailsarray objectAtIndex:0]valueForKey:@"Designation"], 
  [[_contactdetailsarray objectAtIndex:0]valueForKey:@"Department"]];

    }

    cell.lblcontatname.text=[[_contactdetailsarray 
   objectAtIndex:0]valueForKey:@"ContactName"];
    cell.lblcontactnumber.text=[[_contactdetailsarray 
  objectAtIndex:0]valueForKey:@"TelephoneNo"];
    cell.lblemail.text=[[_contactdetailsarray 
  objectAtIndex:0]valueForKey:@"EmailAddress"];
   }

else{


  if(([[[_contactdetailsarray 
objectAtIndex:0]valueForKey:@"Designation"]isEqualToString:@""])&& . 
([[[_contactdetailsarray 
 objectAtIndex:0]valueForKey:@"Department"]isEqualToString:@"" ])){
        cell.lbldesignation.text=@"";

    }


    else{
        cell.lbldesignation.text=[NSString stringWithFormat:@"%@,%@", 
       [[_contactdetailsarray 
     objectAtIndex:indexPath.section]valueForKey:@"Designation"], 
    [[_contactdetailsarray 
     objectAtIndex:indexPath.section]valueForKey:@"Department"]];
         }

      cell.lblcontatname.text=[[_contactdetailsarray 
       objectAtIndex:indexPath.row]valueForKey:@"ContactName"];
    cell.lblcontactnumber.text=[[_contactdetailsarray 
    objectAtIndex:indexPath.row]valueForKey:@"TelephoneNo"];
    cell.lblemail.text=[[_contactdetailsarray 
     objectAtIndex:indexPath.row]valueForKey:@"EmailAddress"];
         }


        return cell;

       }



     - (CGSize)collectionView:(UICollectionView *)collectionView
              layout:(UICollectionViewLayout *)collectionViewLayout
          sizeForItemAtIndexPath:(NSIndexPath *)indexPath
      {




          return CGSizeMake(_collView.bounds.size.width 
            ,_collView.bounds.size.height);

        }

    - (CGFloat)collectionView:(UICollectionView *)collectionView
               layout:(UICollectionViewLayout*)collectionViewLayout
       minimumLineSpacingForSectionAtIndex:(NSInteger)section
    {
    return 5;
    }

     - (NSInteger)collectionView:(UICollectionView *)collectionView 
     numberOfItemsInSection:(NSInteger)section{

    // return 5;


       if(_contactdetailsarray.count==0){
        return 0;
       }

     else{
     return _contactdetailsarray.count;
         }

    }

谢谢和问候, Roshan.k

标签: iosobjective-cuitableviewuicollectionviewuicollectionviewcell

解决方案


推荐阅读