CustomTableCell 中的 UICollectionView
UICollectionView inside CustomTableCell
我在 tableView 的自定义单元格内尝试使用自定义单元格实现 UICollectionView 时遇到了一些问题。
我有自定义 table 视图单元格,它工作正常,显示我想要的标签。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
TemperatureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath: indexPath];
Node *node = [[Node alloc] init];
if(_nodes != nil){
node = [_nodes objectAtIndex:indexPath.row];
if(_tempSensorsDictionary.count > 0){
NSArray *allSensors = [_tempSensorsDictionary objectForKey:node.number];
TemperatureSensor *ts = allSensors[0];
if(node.name != nil && ![node.name isEqual: @""]){
cell.unitNameLabel.text = node.name;
} else {
cell.unitNameLabel.text = node.number;
}
cell.collection = [_tempSensorsDictionary objectForKey:node.number];
}
}
return cell;
}
我已将 CollectionView 背景设置为灰色,我可以看到 "box"。所以我猜 CollectionView 在我的 TemperatureTaleViewCell
class 中正确初始化,我在其中放置:
@implementation TemperatureTableViewCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (!(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) return nil;
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
[flowLayout setItemSize:CGSizeMake(50, 50)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
self.collectionView = [[CollectionView alloc] initWithFrame: CGRectZero collectionViewLayout:flowLayout];
[self.collectionView registerClass:[TemperatureItemCollectionViewCell class] forCellWithReuseIdentifier:@"TemperatureItemCollectionCell"];
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.collectionView reloadData];
[self.contentView addSubview:self.collectionView];
return self;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 2;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
TemperatureItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TemperatureItemCollectionCell" forIndexPath:indexPath];
cell.tempValTempCollViewCell.text = @"21oC";
cell.backgroundColor = [UIColor redColor];
return cell;
}
@end
但是我的 table 视图看起来像这样:
我的代码有什么问题,方向错在哪里?
如我所见,您通过 XIB/storyboard 设计了 TemperatureTaleViewCell,并通过
创建了单元格
[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath: indexPath];
到目前为止没问题,但是您在 initWithStyle 中设置了 collectionView 的委托和数据源,在这种情况下不会调用它们。因此您的集合视图的委托和数据源方法将不会被调用。
需要在 tableview cellForRowAtIndexPath 方法中重新加载集合视图数据。
-(void)tableView:(UITableView *)tableView willDisplayCell:(CategoryCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setCollectionViewDelegate:self indexPath:indexPath];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
TemperatureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath: indexPath];
Node *node = [[Node alloc] init];
if(_nodes != nil){
node = [_nodes objectAtIndex:indexPath.row];
if(_tempSensorsDictionary.count > 0){
NSArray *allSensors = [_tempSensorsDictionary objectForKey:node.number];
TemperatureSensor *ts = allSensors[0];
if(node.name != nil && ![node.name isEqual: @""]){
cell.unitNameLabel.text = node.name;
} else {
cell.unitNameLabel.text = node.number;
}
cell.collection = [_tempSensorsDictionary objectForKey:node.number];
//Reload collection view data
[cell.collectionView reloadData];
}
}
return cell;
}
使用 awakeFromNib 方法代替 table 查看 initWithStyle 方法。
- (void)awakeFromNib
{
[super awakeFromNib];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
[flowLayout setItemSize:CGSizeMake(50, 50)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
self.collectionView = [[CollectionView alloc] initWithFrame: CGRectZero collectionViewLayout:flowLayout];
[self.collectionView registerClass:[TemperatureItemCollectionViewCell class] forCellWithReuseIdentifier:@"TemperatureItemCollectionCell"];
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.contentView addSubview:self.collectionView];
}
在 TemperatureTaleViewCell 中设置集合视图的委托 class
- (void)setCollectionViewDelegate:(id)dataSourceDelegate indexPath:(NSIndexPath *)indexPath
{
self.collectionView.delegate = dataSourceDelegate;
}
我在 tableView 的自定义单元格内尝试使用自定义单元格实现 UICollectionView 时遇到了一些问题。 我有自定义 table 视图单元格,它工作正常,显示我想要的标签。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
TemperatureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath: indexPath];
Node *node = [[Node alloc] init];
if(_nodes != nil){
node = [_nodes objectAtIndex:indexPath.row];
if(_tempSensorsDictionary.count > 0){
NSArray *allSensors = [_tempSensorsDictionary objectForKey:node.number];
TemperatureSensor *ts = allSensors[0];
if(node.name != nil && ![node.name isEqual: @""]){
cell.unitNameLabel.text = node.name;
} else {
cell.unitNameLabel.text = node.number;
}
cell.collection = [_tempSensorsDictionary objectForKey:node.number];
}
}
return cell;
}
我已将 CollectionView 背景设置为灰色,我可以看到 "box"。所以我猜 CollectionView 在我的 TemperatureTaleViewCell
class 中正确初始化,我在其中放置:
@implementation TemperatureTableViewCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (!(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) return nil;
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
[flowLayout setItemSize:CGSizeMake(50, 50)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
self.collectionView = [[CollectionView alloc] initWithFrame: CGRectZero collectionViewLayout:flowLayout];
[self.collectionView registerClass:[TemperatureItemCollectionViewCell class] forCellWithReuseIdentifier:@"TemperatureItemCollectionCell"];
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.collectionView reloadData];
[self.contentView addSubview:self.collectionView];
return self;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 2;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
TemperatureItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TemperatureItemCollectionCell" forIndexPath:indexPath];
cell.tempValTempCollViewCell.text = @"21oC";
cell.backgroundColor = [UIColor redColor];
return cell;
}
@end
但是我的 table 视图看起来像这样:
我的代码有什么问题,方向错在哪里?
如我所见,您通过 XIB/storyboard 设计了 TemperatureTaleViewCell,并通过
创建了单元格[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath: indexPath];
到目前为止没问题,但是您在 initWithStyle 中设置了 collectionView 的委托和数据源,在这种情况下不会调用它们。因此您的集合视图的委托和数据源方法将不会被调用。
需要在 tableview cellForRowAtIndexPath 方法中重新加载集合视图数据。
-(void)tableView:(UITableView *)tableView willDisplayCell:(CategoryCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setCollectionViewDelegate:self indexPath:indexPath];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
TemperatureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath: indexPath];
Node *node = [[Node alloc] init];
if(_nodes != nil){
node = [_nodes objectAtIndex:indexPath.row];
if(_tempSensorsDictionary.count > 0){
NSArray *allSensors = [_tempSensorsDictionary objectForKey:node.number];
TemperatureSensor *ts = allSensors[0];
if(node.name != nil && ![node.name isEqual: @""]){
cell.unitNameLabel.text = node.name;
} else {
cell.unitNameLabel.text = node.number;
}
cell.collection = [_tempSensorsDictionary objectForKey:node.number];
//Reload collection view data
[cell.collectionView reloadData];
}
}
return cell;
}
使用 awakeFromNib 方法代替 table 查看 initWithStyle 方法。
- (void)awakeFromNib
{
[super awakeFromNib];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
[flowLayout setItemSize:CGSizeMake(50, 50)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
self.collectionView = [[CollectionView alloc] initWithFrame: CGRectZero collectionViewLayout:flowLayout];
[self.collectionView registerClass:[TemperatureItemCollectionViewCell class] forCellWithReuseIdentifier:@"TemperatureItemCollectionCell"];
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.contentView addSubview:self.collectionView];
}
在 TemperatureTaleViewCell 中设置集合视图的委托 class
- (void)setCollectionViewDelegate:(id)dataSourceDelegate indexPath:(NSIndexPath *)indexPath
{
self.collectionView.delegate = dataSourceDelegate;
}