Ios UICollectionView在CustomTableCell内
在tableView中的自定义单元格中使用自定义单元格实现UICollectionView时,我遇到了一些问题。 我有一个自定义的表格视图单元格,它可以很好地显示我想要的标签Ios UICollectionView在CustomTableCell内,ios,objective-c,uitableview,uicollectionview,uicollectionviewcell,Ios,Objective C,Uitableview,Uicollectionview,Uicollectionviewcell,在tableView中的自定义单元格中使用自定义单元格实现UICollectionView时,我遇到了一些问题。 我有一个自定义的表格视图单元格,它可以很好地显示我想要的标签 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { TemperatureTableViewCell *cell = [tableView dequeueReu
- (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背景设置为灰色,我可以看到这个“框”。因此,我猜CollectionView在我的TemperatureTaleViewCell
类中已正确初始化,我在其中放置了:
@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
但我的表视图如下所示:
我的代码出了什么问题?方向哪里错了?正如我所见,您通过XIB/故事板设计了TemperatureTaleViewCell,并通过
[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath: indexPath];
到目前为止还可以,但您正在initWithStyle中设置collectionView的委托和数据源,在本例中不会调用它。因此不会调用集合视图的委托和数据源方法 如我所见,您通过XIB/情节提要设计了TemperatureTaleViewCell,并通过
[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath: indexPath];
到目前为止还可以,但您正在initWithStyle中设置collectionView的委托和数据源,在本例中不会调用它。因此不会调用集合视图的委托和数据源方法 需要在tableviewcellForRowAtIndexPath方法中重新加载集合视图数据
-(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方法代替表视图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];
}
在类中设置集合视图的委托
- (void)setCollectionViewDelegate:(id)dataSourceDelegate indexPath:(NSIndexPath *)indexPath
{
self.collectionView.delegate = dataSourceDelegate;
}
需要在tableviewcellForRowAtIndexPath方法中重新加载集合视图数据
-(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方法代替表视图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];
}
在类中设置集合视图的委托
- (void)setCollectionViewDelegate:(id)dataSourceDelegate indexPath:(NSIndexPath *)indexPath
{
self.collectionView.delegate = dataSourceDelegate;
}
请仔细阅读此文档并更新您的答案如果您为指定的标识符注册了一个类并且必须创建一个新的单元格,此方法通过调用其initWithStyle:reuseIdentifier:method来初始化单元格。那么我是否应该插入If(cell==nil){cell=[[TemperatureTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:@“Cell”];}在TemperatureTableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“Cell”forIndexPath:indexPath]@对于初始化,您的方法是正确的。不要插入它。@trungduc:Nah,如果我没有注册单元格的类(通过代码),只需将单元格直接添加到Xib/storyboard上的UITableView中,dequeueReusableCellWithIdentifier将不会调用initWithStyle:reuseIdentifier方法。如果您为指定标识符注册了类并且必须创建新单元格,请仔细阅读本文档并更新您的答案,此方法通过调用其initWithStyle:reuseIdentifier:method来初始化单元格。因此,我应该在TemperatureTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@“cell”之后插入if(cell==nil){cell=[[TemperatureTableViewCellAlloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@“cell”;}吗forIndexPath:indexPath]@对于初始化,您的方法是正确的。不要插入它。@trungduc:Nah,如果我没有注册单元格的类(通过代码)并直接将单元格添加到Xib/storyboard上的UITableView中,则DequeueableReuseCellWithIdentifier将不会调用initWithStyle:reuseIdentifier方法。不幸的是,这没有帮助。重载是在CustomCell init方法中完成的。此方法与注释中提供的另一个方法一样有效。哪种方法更好?有什么区别?不幸的是,没有帮助。重载是在CustomCell init方法中完成的。此方法与注释中提供的另一个方法一样有效。哪种方式更好,有什么区别?