2017-02-01 7 views
0

に埋め込まれたUICollectionViewという単純なアプリを作った。 しかし、アプリケーションが実行されると、UICollectionViewCellは色が割り当てられず、後で再利用されます。背景色が割り当てられておらず、セル再利用の問題

TableViewCellCollectionViewCellは、そのペン先にそれぞれ設計されている - CustomTableViewCell.xibCustomCollectionViewCell.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の画像は、プロジェクト

enter image description here

どのように私はこの問題を解決しないを実行した後で? 助けてください。

同じ列のセルは、TableViewの第1列の赤色セル、TableViewの第2列の緑色セルなど同じ色を持たなければなりません。あなたのUITableViewDelegate方法で

+0

を下回っているあなたはcolletionviewcellの背景やtableviewcell年代の色を変更しますか? – KKRocks

+0

CollectionViewCellの背景色を変更する必要があります。 @KKRocks – bhatejaud

+1

この行を置き換える必要があります:cCell.backgroundColor = [_ color objectAtIndex:indexPath.row];コレクションビューのcellForItemAtIndexPathでは、tableviewのcellForItemAtIndexPathではなく、 – KKRocks

答えて

0

は、あなたのcollectionViewセルの背景を設定cellForItemAtIndexPathUICollectionViewDelegate方法では、その後collectionViewCellとセットアップそのdelegatedatasourceを登録cellForRow。コントローラでcollectionViewデリゲートを割り当てると、管理しやすくなります。

希望します。

0

私にあなたのメールIDを教えてくださいこれに関してあなたにデモを送付します。

マイ出力

enter image description here

+0

[email protected] – bhatejaud

関連する問題