Objective c 将CollectionView插入ios中的TableView单元格

Objective c 将CollectionView插入ios中的TableView单元格,objective-c,Objective C,我有一个4排的桌面视图。然后我想在tableview单元格中插入一个collectionview,每个collectionview都有不同的单元格。但我不能那样做。有人能帮我吗?或者有什么建议吗?您可以为每个TableViewCell指定不同的CellIdentifier,并在InterfaceBuilder或inside中创建不同的行 cellForRowAtIndexPath 不要忘记为将为TableViewCell创建的每个collectionView提供数据源和委托 @property(

我有一个4排的桌面视图。然后我想在tableview单元格中插入一个collectionview,每个collectionview都有不同的单元格。但我不能那样做。有人能帮我吗?或者有什么建议吗?

您可以为每个TableViewCell指定不同的CellIdentifier,并在InterfaceBuilder或inside中创建不同的行 cellForRowAtIndexPath

不要忘记为将为TableViewCell创建的每个collectionView提供
数据源
委托

@property(strong,nonatomic)IBOutlet UICollectionView *clview;
伪代码:

YN是指你的名字空间

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

此代码将帮助您

yourViewController.h

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


    {
    IBOutlet UITableView *tbleeView;

    }
您的TableViewCell.h(UITableViewCell)

yourViewController.m

    - (void)viewDidLoad
    {
    [super viewDidLoad];

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

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

     }

每个collectionview都有不同的元素。。。。就像一个单元格只有imageview,另一个collectionview单元格只有标签等。。。。那么,有人可以帮我吗?但我想创建一个tableview单元格和一个collectionview单元格。然后我使用(indexpath.row==something)重用了一个tableview单元格。但还有一件事,每个集合视图都有不同的元素。我可以这样做吗?你明白我的问题了吗?那就使用TableViewCell的单一标识符,你能把你的技术要求发给我吗?嗨@ekiyanov,好久不见了。。。我想要一个像android app store这样的布局设计,collectionview拥有每个tableview单元格和不同的数据。你能理解我的要求吗。我扔了很多教程,但每个教程都有swift语言,但我不能使用swift语言,你能在那种类型的UI设计中帮助objective c吗?另外,请转到所有表视图单元格中是否要加载集合视图?是的。。。。但我希望每个tableview行显示不同的颜色。。。如第一个表格视图行开始为红色,第二行开始为白色等。。有可能吗?我想如果你的代码可以工作,那么每个tableview单元格都会显示相同的输出。。。。但是试着去理解。。。我想要不同颜色的第二个tableview单元格,第三个tableview单元格等等。。。有可能吗?是的,您可以更改每个单元格的背景颜色。。用这个代码。我已经在我的代码中使用了它,它工作得非常好。您可以更改表格视图单元格背景颜色的颜色。第二:最简单的方法是点击Web服务,将响应放入数组中,然后重新加载表视图。@Dishant,试着理解一下。我将一个collectionview插入tableview单元格,即完成。想象一下tableview有4个单元格/行。。。然后,我想在每个集合视图中显示不同的内容(在tableview单元格下…,如android store…)。。。。请帮我解决这个问题。您的代码正在工作,但输出与我所看到的不相似。