Ios 未分配背景色和单元格重用问题

Ios 未分配背景色和单元格重用问题,ios,objective-c,uitableview,uicollectionview,Ios,Objective C,Uitableview,Uicollectionview,我制作了一个简单的应用程序,其中UICollectionView嵌入UITableViewCell。 但是当应用程序运行时,UICollectionViewCell不会到达指定的颜色,以后会被重用 TableViewCell和CollectionViewCell分别在其nib-CustomTableViewCell.xib和CustomCollectionViewCell.h 同一行中的单元格必须具有相同的颜色,即TableView第一行中的红色单元格、TableView第二行中的绿色单元格等等

我制作了一个简单的应用程序,其中
UICollectionView
嵌入
UITableViewCell
。 但是当应用程序运行时,
UICollectionViewCell
不会到达指定的颜色,以后会被重用

TableViewCellCollectionViewCell分别在其nib-
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
@interface CustomTableViewCell : UITableViewCell  <UICollectionViewDelegate, UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UICollectionView   *userCollectionView;
@end
CustomTableViewCell.h

@interface ViewController:UIViewController <UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *userTableView;
@property(strong,nonatomic) NSArray* color;
@end
@interface CustomTableViewCell : UITableViewCell  <UICollectionViewDelegate, UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UICollectionView   *userCollectionView;
@end
下面是运行项目后的UI图像

我如何解决这个问题? 请帮忙


同一列中的单元格必须具有相同的颜色,即TableView第1列中的红色单元格,TableView第二列中的绿色单元格依此类推。

在您的
UITableViewDelegate
方法
cellForRow
中注册collectionViewCell并设置其
delegate
datasource
然后在
UICollectionViewDelegate
方法
CellForItemIndexPath
中设置背景您的collectionView单元格的。如果在控制器中分配collectionView代理,则管理起来会更好、更容易


希望有帮助。

您想更改CollectionViewCell的背景色还是tableviewcell的背景色?CollectionViewCell的背景色应该更改@KKRocksthen您需要替换此行:cCell.backgroundColor=[\u color objectAtIndex:indexPath.row];在collectionview的cellForItemAtIndexPath中,而不是在tableview的cellForItemAtIndexPath中。同意@KKRocks,并且tableview cellForRow必须在其集合视图上重新加载数据。它确实可以工作,但我希望表视图的每一行只显示一种颜色的单元格,即第1行的所有单元格显示红色,第2行的单元格显示绿色,依此类推@KKRocks