我制作了一个简单的应用,UICollectionView
嵌入了UITableViewCell
。
但是,当应用程序运行时,UICollectionViewCell
不会为此指定颜色,稍后会重复使用。
TableViewCell 和 CollectionViewCell 分别设计在他们的笔尖中 - CustomTableViewCell.xib
和CustomCollectionViewCell.h
同一行中的单元格必须具有相同的颜色,即TableView第一行中的红色单元格,TableView第二行中的绿色单元格等等。
我的代码在
下面ViewController.h
@interface ViewController:UIViewController <UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *userTableView;
@property(strong,nonatomic) NSArray* color;
@end
ViewController.m
- (void)viewDidLoad{
[super viewDidLoad];
_color=[[NSArray alloc] initWithObjects:[UIColor redColor],[UIColor greenColor],[UIColor blueColor],[UIColor purpleColor],[UIColor orangeColor],[UIColor cyanColor],[UIColor lightGrayColor],[UIColor magentaColor], nil];
[_userTableView registerNib:[UINib nibWithNibName:@"CustomTableViewCell" bundle:nil] forCellReuseIdentifier:@"TVCell"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 8;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TVCell"];
CustomCollectionViewCell *cCell=(CustomCollectionViewCell*)[cell.userCollectionView cellForItemAtIndexPath:indexPath];
cCell.backgroundColor=[_color objectAtIndex:indexPath.row];
return cell;
}
CustomTableViewCell.h
@interface CustomTableViewCell : UITableViewCell <UICollectionViewDelegate, UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UICollectionView *userCollectionView;
@end
CustomTableViewCell.m
@implementation CustomTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
[_userCollectionView registerNib:[UINib nibWithNibName:@"CustomCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CVCell"];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 20;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CustomCollectionViewCell* cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CVCell" forIndexPath:indexPath];
return cell;
}
以下是运行项目后的UI图像
我该如何解决这个问题? 请帮忙。
同一列中的单元格必须具有相同的颜色,即TableView第一列中的红色单元格,TableView第二列中的绿色单元格等等。
答案 0 :(得分:0)
在UITableViewDelegate
方法cellForRow
中注册collectionViewCell并设置其delegate
和datasource
,然后在UICollectionViewDelegate
方法cellForItemAtIndexPath
中设置背景你的collectionView Cell。如果在控制器中分配collectionView委托,将会更好,更容易管理。
希望它有所帮助。