に埋め込まれたUICollectionView
という単純なアプリを作った。 しかし、アプリケーションが実行されると、UICollectionViewCell
は色が割り当てられず、後で再利用されます。背景色が割り当てられておらず、セル再利用の問題
TableViewCellとCollectionViewCellは、そのペン先にそれぞれ設計されている - CustomTableViewCell.xib
とCustomCollectionViewCell.h
細胞を同じ行のテーブルビューの1行目、2回目に緑色細胞における赤色セル、すなわち同じ色を有していなければなりませんテーブルビューの行など。
マイコードは
を下回る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の第1列の赤色セル、TableViewの第2列の緑色セルなど同じ色を持たなければなりません。あなたのUITableViewDelegate
方法で
を下回っているあなたはcolletionviewcellの背景やtableviewcell年代の色を変更しますか? – KKRocks
CollectionViewCellの背景色を変更する必要があります。 @KKRocks – bhatejaud
この行を置き換える必要があります:cCell.backgroundColor = [_ color objectAtIndex:indexPath.row];コレクションビューのcellForItemAtIndexPathでは、tableviewのcellForItemAtIndexPathではなく、 – KKRocks