如何在iOS中的自定义TableViewCell内部实现UICollectionView的委托和数据源方法,目标C [英] How to implement delegate and datasource methods for UICollectionView when it is inside a custom TableViewCell in iOS, objective C

查看:97
本文介绍了如何在iOS中的自定义TableViewCell内部实现UICollectionView的委托和数据源方法,目标C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我不想使用任何第三方库


  • 我有 tableview 带有自定义表格视图单元格(表格视图工作正常)。

  • 现在在表格视图单元格内我想实现集合视图

  • 现在我的问题是,我应该在哪里实现集合视图的委托方法和数据源方法自定义表格视图单元格。以下是我的尝试。

  • I have tableview with with a custom table view cell (table view working fine).
  • Now inside the table view cell I want to implement a collection view.
  • Now my problem is , where should I implement the delegate methods and data source methods for the collection view, which is inside the custom table view cell . below is what I have tried.

Tabel View Controller实施

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

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

    return cell;
}

正常工作

现在在 FirstTableViewCell 里面有一个集合视图

这是我的FirstTableViweCell.h文件

#import <UIKit/UIKit.h>

@interface FirstTableViewCell : UITableViewCell<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic, nullable) IBOutlet UICollectionView *insideCollectionView;

@property (strong, nonnull,nonatomic) NSArray *mycollectionData;
- (void)setColletionData :(nonnull NSArray *)collectionData;

@end

这是我的FirstTableViewCell.m文件

#import "FirstTableViewCell.h"

@implementation FirstTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.insideCollectionView.delegate = self;

    // Initialization code
}

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

    // Configure the view for the selected state
}

- (void)setColletionData:(NSArray *)collectionData
{
    self.mycollectionData = collectionData;
    [self.insideCollectionView reloadData];
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *ccell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cvcell" forIndexPath:indexPath];
    return ccell;
}

@end

以下方法

- (void)setColletionData:(NSArray *)collectionData
    {
        self.mycollectionData = collectionData;
        [self.insideCollectionView reloadData];
    }

我曾经设置数组 for collectionView cellforrowatindexpath in tableview

I used to set the array for collectionView, in cellforrowatindexpath in tableview


那么实现 DataSource 的正确方法是什么?为 CollectionView 委派
方法,该方法位于自定义表
查看单元格


推荐答案

Horraaaaaaaay ..... = D这是完成的回答这个问题。



表格查看实施.m文件

#import "ViewController.h"
#import "FirstTableViewCell.h"

@interface ViewController ()

@end

@implementation ViewController
{
    NSArray *bbarray;

}

- (void)viewDidLoad {
    [super viewDidLoad];

    bbarray = [NSArray arrayWithObjects:@"33",@"44", nil];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

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

    [cell setColletionData:bbarray];

    return cell;
}

@end

在自定义表格查看单元格

自定义表格视图.h文件

#import <UIKit/UIKit.h>
@interface FirstTableViewCell : UITableViewCell<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic, nullable) IBOutlet UICollectionView *insideCollectionView;

@property (strong, nonnull,nonatomic) NSArray *mycollectionData;
- (void)setColletionData :(nonnull NSArray *)collectionData;

@end

自定义表格视图.m文件

#import "FirstTableViewCell.h"

@implementation FirstTableViewCell


- (void)awakeFromNib {
    [super awakeFromNib];

    // These two are very important.
    self.insideCollectionView.delegate = self;
    self.insideCollectionView.dataSource = self;

    // Initialization code
}

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

    // Configure the view for the selected state
}


//this method is used to set the data , call it in cell for row at index path in table view implementation.
    - (void)setColletionData:(NSArray *)collectionData
    {
        self.mycollectionData = collectionData;
        [self.insideCollectionView reloadData];
    }


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@"%lu", (unsigned long)self.mycollectionData.count);
    return [self.mycollectionData count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *ccell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cvcell" forIndexPath:indexPath];
    return ccell;
}

@end

这篇关于如何在iOS中的自定义TableViewCell内部实现UICollectionView的委托和数据源方法,目标C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆