将CollectionView插入ios中的TableView Cell

时间:2017-03-16 12:57:18

标签: objective-c

我有一个有4行的tableview。然后我想将一个collectionview插入tableview单元格,每个collectionview都有不同的单元格。但我无法做到这一点。任何人都可以帮助我或任何建议吗?

2 个答案:

答案 0 :(得分:0)

您可以为每个TableViewCell提供不同的CellIdentifier,并在InterfaceBuilder或内部创建不同的行 的cellForRowAtIndexPath

不要忘记为您将为TableViewCell创建的每个collectionView提供dataSourcedelegate

伪代码:

YN表示YourNamespace

YNDataSourceDelegate1* data1;
YNDataSourceDelegate2* data2;
YNDataSourceDelegate3* data3;
YNDataSourceDelegate4* data4;

const NSInteger COLLECTION_VIEW_TAG = 1001;

NSArray* providers=@[data1,data2,data3,data4];
NSArray* rowIds=@[@"row1",@"row2",@"row3",@"row4"];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:rowIds[indexPath.row]];

UICollectionView * collectionView = (UICollectionView*)[cell viewWithTag:COLLECTION_VIEW_TAG];

collectionView.dataSource = providers[indexPath.row];
return cell;
}

答案 1 :(得分:0)

此代码可以帮助您

<强> yourViewController.h

    @interface testingvc : UIViewController
    <
    UITableViewDelegate,
    UITableViewDataSource,
    UICollectionViewDelegate,
    UICollectionViewDataSource
    >


    {
    IBOutlet UITableView *tbleeView;

    }

<强> yourViewController.m

    - (void)viewDidLoad
    {
    [super viewDidLoad];

    tbleeView.delegate=self;
    tbleeView.dataSource=self;
    tbleeView.pagingEnabled=YES;

    }

<强> yourTableViewCell.h (适用的UITableViewCell)

@property(strong,nonatomic)IBOutlet UICollectionView *clview;

<强> yourViewController.m

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    //#warning Incomplete implementation, return the number of sections
    return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    //#warning Incomplete implementation, return the number of rows

    return 10;

    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath
    {
    static  NSString *identifierDriver = @"abcdefgh";

    yourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierDriver forIndexPath:indexPath];

//testing table cell is a UITableViewCell

    if (cell == nil)
    {
        cell = [[testingTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierDriver];
    }

    cell.backgroundColor=[UIColor yellowColor]; 

//@property(strong,nonatomic)IBOutlet UICollectionView *clview;-this method is implement in UITableViewCell

// call collection view delegate method in table view cell.

    cell.clview.delegate=self;
    cell.clview.dataSource=self;
    cell.clview.parentIndexpath = indexPath;
    cell.clview.backgroundColor=[UIColor magentaColor];
    cell.clview.pagingEnabled=YES;//optional



    return cell;
    }

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        NSArray *collectionViewArray =  [_arrayProducts [[(IndexedCollectionView *)collectionView parentIndexpath].row]objectForKey:@"inventory"];//arrProducts is an array where i get all the data from webservice

    return [collectionViewArray count];

    }
    - (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point

    {
    return 0;
    }
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    yourColloectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"abcdef" forIndexPath:indexPath];

    NSArray *Products  = [_arrayProducts[[(yourCollectionView *)collectionView parentIndexpath].row]objectForKey:@"inventory"];//arrProducts is an array where i get all the data from webservice


      NSLog(@"object ibde xis ----%ld",[Products count]);
     if (indexPath.row>[Products count]-1 )
     {
    NSLog(@"index not found");

     }

      else
    {

      NSDictionary *dictdata = [Products objectAtIndex:indexPath.row];
     NSLog(@"dictdata is =====%@",dictdata);

   //do your code here


    }


    if (indexPath.row % 2)
    {
    cell.backgroundColor=[UIColor brownColor];;
    }
    else
    cell.backgroundColor=[UIColor redColor];;

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


    - (void)collectionView:(IndexedCollectionView *)collectionView
     didSelectItemAtIndexPath:(NSIndexPath *)indexPath

    {



     NSInteger tableindex = collectionView.parentIndexpath.row;

    NSLog(@"collection path row is ----%ld",tableindex);
     NSLog(@"collection path row is ----%ld",indexPath.row);

    //you can get all that index path number . Enjoy :)

     }