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

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

问题描述

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

  • 我有 tableview 和自定义 table view cell(table view 工作正常).
  • 现在在table view cell里面我想实现一个collection view.
  • 现在我的问题是,我应该在哪里为集合视图实现 delegate 方法和 data source 方法,它位于 custom table view cell .以下是我尝试过的.

表视图控制器实现

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{返回 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{返回 5;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tvcell"];返回单元格;}

正常工作

现在在这个FirstTableViewCell里面有一个collection view.

这是我的 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)setCollectionData :(nonnull NSArray *)collectionData;@结尾

这是我的 FirstTableViewCell.m 文件

#import "FirstTableViewCell.h"@implementation FirstTableViewCell- (void)awakeFromNib {[超级清醒的笔尖];self.insideCollectionView.delegate = self;//初始化代码}- (void)setSelected:(BOOL)选定动画:(BOOL)animated {[超级setSelected:选定动画:动画];//配置选中状态的视图}- (void)setCollectionData:(NSArray *)collectionData{self.mycollectionData = 集合数据;[self.insideCollectionView reloadData];}- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{返回 1;}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{返回 10;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{UICollectionViewCell *ccell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cvcell" forIndexPath:indexPath];返回 ccell;}@结尾

以下方法

- (void)setCollectionData:(NSArray *)collectionData{self.mycollectionData = 集合数据;[self.insideCollectionView reloadData];}

我曾经在tableview中的cellforrowatindexpath中为collectionView设置array<块引用>

那么实现DataSourceDelegate的正确方法是什么Custom Table 中的 CollectionView 的方法查看单元格.

解决方案

Horraaaaaaaay ..... =D 这是完整的答案.

表视图实现 .m 文件

#import "ViewController.h"#import FirstTableViewCell.h"@interface 视图控制器 ()@结尾@implementation 视图控制器{NSArray *bbarray;}- (void)viewDidLoad {[超级视图DidLoad];bbarray = [NSArray arrayWithObjects:@"33",@"44", nil];//在加载视图后做任何额外的设置,通常是从一个 nib.}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];//处理所有可以重新创建的资源.}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{返回 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{返回 5;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tvcell"];[单元集集合数据:bbarray];返回单元格;}@结尾

自定义表格视图单元格内的实现

自定义表格视图 .h 文件

#import <UIKit/UIKit.h>@interface FirstTableViewCell : UITableViewCell<UICollectionViewDelegate,UICollectionViewDataSource>@property (weak, nonatomic, nullable) IBOutlet UICollectionView *insideCollectionView;@property (strong, nonnull,nonatomic) NSArray *mycollectionData;- (void)setCollectionData :(nonnull NSArray *)collectionData;@结尾

自定义表格视图 .m 文件

#import "FirstTableViewCell.h";@implementation FirstTableViewCell- (void)awakeFromNib {[超级清醒的笔尖];//这两个很重要.self.insideCollectionView.delegate = self;self.insideCollectionView.dataSource = self;//初始化代码}- (void)setSelected:(BOOL)选定动画:(BOOL)animated {[超级setSelected:选定动画:动画];//配置选中状态的视图}//此方法用于设置数据,在表格​​视图实现中在索引路径处的行的单元格中调用它.- (void)setCollectionData:(NSArray *)collectionData{self.mycollectionData = 集合数据;[self.insideCollectionView reloadData];}- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{返回 1;}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{NSLog(@"%lu", (unsigned long)self.mycollectionData.count);返回 [self.mycollectionData 计数];}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{UICollectionViewCell *ccell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cvcell"forIndexPath:indexPath];返回 ccell;}@结尾

Note : I don't want to use any third party libraries

  • 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 implementation

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

working properly

now inside this FirstTableViewCell there is a collection view.

This is my FirstTableViweCell.h File

#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

and this is my FirstTableViewCell.m File

#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

following method

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

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

So what is the correct way to implement DataSource and Delegate methods for the CollectionView which is inside the Custom Table View Cell.

解决方案

Horraaaaaaaay ..... =D This is the completed answer for this.

Table View Implementation .m file

#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

Implementation inside the Custom Table View cell

custom table view .h file

#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

custom table view .m file

#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

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

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