1

私はUICollectionViewを持っていて、collectionViewCelllabelimageを設定しようとしています。残念ながら私はそれについて表示するために、または何か他のラベルを得るように思わない。カスタムUICollectionViewCellを作成する方法

は、ここに私のコードです:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; 

    UILabel *issue = [[UILabel alloc] initWithFrame:CGRectMake(0,10,cell.bounds.size.width,40)]; 
    if(indexPath.item %2 == 0){ 
     cell.backgroundColor=[UIColor blueColor]; 
     issue.text = @"Some Text"; 
     issue.textColor = [UIColor greenColor]; 
     issue.textAlignment = NSTextAlignmentCenter; 
    } 
    else { 
     cell.backgroundColor=[UIColor redColor]; 
     issue.text = @"Some Text"; 
     issue.textColor = [UIColor greenColor]; 
     issue.textAlignment = NSTextAlignmentCenter; 
    } 
} 

は、残念ながら、ラベルが表示されていないと、どちらもラベルのテキストではありません。

更新:このクラスファイルの残りのコードを追加しました。

#import "ContainerListController.h" 
#import "ContainerController.h" 
#import "ContainerList.h" 


@implementation ContainerListController 


//Deallocate temp variables 
- (void)dealloc { 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

//Initiate objects 
- (id)init { 
    if (self = [super initWithTitle:LocStr(@"CONTAINER_LIST_TITLE") navBarHidden:NO]) { 
     m_paths = [ContainerList shared].paths; 

     [[NSNotificationCenter defaultCenter] addObserver:self 
      selector:@selector(onContainerListDidChange) 
      name:kSDKLauncherContainerListDidChange object:nil]; 
    } 

    return self; 
} 

//Load all the views. 
- (void)loadView { 
    //Allocate a UI view 
    self.view = [[UIView alloc] init]; 

    //Create flow layout 
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; 

    //Force Horizontal Scroll 
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; 
    layout.minimumInteritemSpacing =[[UIScreen mainScreen] bounds].size.width; 
    layout.minimumLineSpacing=0.0; 


    //Create Collection 
    UICollectionView *coll =[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout]; 

    //Allocations 
    m_coll = coll; 
    coll.dataSource =self; 
    coll.delegate =self; 
    coll.pagingEnabled = YES; 
    coll.collectionViewLayout = layout; 

    //Customize Cells 
    [coll registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"]; 
    [coll setBackgroundColor:[UIColor orangeColor]]; 

    [layout invalidateLayout]; 

    //Create the subview 
    [self.view addSubview:coll]; 




    //set minimum spacing 
    /*if(UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)){ 
     NSLog(@"Changed to landscape Spacing"); 
     layout.minimumLineSpacing = 100.0f; 
     layout.minimumInteritemSpacing = 100.0f; 
    } 
    else{ 

     layout.minimumLineSpacing = 40.0f; 
     layout.minimumInteritemSpacing = 40.0f; 
    }*/ 

    //Old Layout 
    //UITableView *table = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; 
    //m_table = table; 
    //table.dataSource = self; 
    //table.delegate = self; 
    //[self.view addSubview:table]; 

} 

- (void)onContainerListDidChange { 
    m_paths = [ContainerList shared].paths; 
    [m_table reloadData]; 
    [m_coll reloadData]; 
} 

//Debugging components function 
/*-(void)printComps:(NSArray*) components{ 
    for (NSInteger i =0; i<16; i++) { 
     NSString * item; 
     item=components[i]; 
    } 
}*/ 

//old tableview cell 
- (UITableViewCell *) 
    tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
     reuseIdentifier:nil]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    NSString *path = [m_paths objectAtIndex:indexPath.row]; 
    NSArray *components = path.pathComponents; 
    cell.textLabel.text = (components == nil || components.count == 0) ? 
     @"" : components.lastObject; 
    return cell; 
} 

//Old tableView 
- (void) 
    tableView:(UITableView *)tableView 
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    NSString *path = [m_paths objectAtIndex:indexPath.row]; 
    ContainerController *c = [[ContainerController alloc] initWithPath:path]; 

    if (c != nil) { 
     [self.navigationController pushViewController:c animated:YES]; 
    } 
    NSLog(@"Selected an item"); 
} 

//old TableView count for epubs 
- (NSInteger) 
    tableView:(UITableView *)tableView 
    numberOfRowsInSection:(NSInteger)section{ 
    return m_paths.count; 
} 

- (void)viewDidLayoutSubviews { 

    //m_table.frame = self.view.bounds; 
    m_coll.frame = self.view.bounds; 
} 

//Collection View Cell Data Allocation 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
        cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; 
    UILabel *issue = [[UILabel alloc] initWithFrame:CGRectMake(0,10,cell.bounds.size.width,40)]; 
    //UICollectionViewCell *content = [[UICollectionViewCell alloc] init]; 
    if(indexPath.item %2 == 0){ 
     cell.backgroundColor=[UIColor blueColor]; 
     issue.text = @"Some Text"; 
     issue.textColor = [UIColor greenColor]; 
     issue.textAlignment = NSTextAlignmentCenter; 
    } 
    else { 
     cell.backgroundColor=[UIColor redColor]; 
     issue.text = @"Some Text"; 
     issue.textColor = [UIColor greenColor]; 
     issue.textAlignment = NSTextAlignmentCenter; 
    } 
    NSString *path = [m_paths objectAtIndex:indexPath.row]; 
    NSArray *components = path.pathComponents; 
    NSString *Title = components.lastObject; 

    NSLog(@"Title: %@",Title); 
    NSString *Titletest = components.lastObject; 
    NSInteger comp1 = components.count; 
    NSString *comps = @"components"; 
    NSLog(@"There are: %ld %@", (long)comp1,comps); 
    NSLog(@"Title: %@",Titletest); 
    for (NSInteger i =0; i<15; i++) { 
    NSString * item; 
    item=components[i]; 
    NSLog(@"Component:%ld %@",(long)i,components[i]); 
    } 

    return cell; 
} 

//Collection View Cell Data De-Allocation 
- (void) 
collectionView:(UICollectionView *)collectionView 
numberofItemsInSection:(NSIndexPath *)indexPath{ 

    [collectionView deselectItemAtIndexPath:indexPath animated:YES]; 
    NSString *path = [m_paths objectAtIndex:indexPath.row]; 
    ContainerController *c = [[ContainerController alloc] initWithPath:path]; 

    if(c !=nil){ 
     [self.navigationController pushViewController:c animated:YES]; 
    } 
} 

//Collection 
-(NSInteger) 
    collectionView:(UICollectionView *)collectionView 
    numberOfItemsInSection:(NSInteger)section{ 

    return m_paths.count; 
} 


//Set Collection View Cell Size 
-(CGSize) 
    collectionView:(UICollectionView *) collectionView 
    layout:(UICollectionViewLayout*)collectionViewLayout 
    sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ 


    //Set Landscape size of cells 
    /*if(UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)){ 
     CGFloat cellWidth = [[UIScreen mainScreen] bounds].size.width-360; 
     CGFloat cellHeigt = [[UIScreen mainScreen] bounds].size.height-60; 
     NSLog(@"Is Landscape"); 
     return CGSizeMake(cellWidth, cellHeigt); 
    } 
    //Set Potrait size of cells 
    else{ 
     CGFloat cellWidth = [[UIScreen mainScreen] bounds].size.width-60; 
     CGFloat cellHeigt = [[UIScreen mainScreen] bounds].size.height-160; 
     NSLog(@"Is Portrait"); 
     return CGSizeMake(cellWidth, cellHeigt); 
    }*/ 

    return CGSizeMake(collectionView.bounds.size.width, collectionView.bounds.size.height); 
} 

//Collection View Cell Position 
- (UIEdgeInsets)collectionView: 
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { 

    if(UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)){ 
     return UIEdgeInsetsMake(150.0,0.0,150.0,0.0); // top, left, bottom, right 
    } 
    else{ 
     return UIEdgeInsetsMake(20.0,0.0,0.0,0.0); // top, left, bottom, right 
    } 
} 

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ 

    [m_coll performBatchUpdates:nil completion:nil]; 
} 

-(void)viewWillTransitionToSize:withTransitionCoordinator{ 

    [m_coll performBatchUpdates:nil completion:nil]; 
} 

/*-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*) 
collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{ 

    CGFloat cellSpacing = ((UICollectionViewFlowLayout *) collectionViewLayout).minimumLineSpacing; 
    CGFloat cellWidth = ((UICollectionViewFlowLayout *) collectionViewLayout).itemSize.width; 
    NSInteger cellCount = [collectionView numberOfItemsInSection:section]; 
    CGFloat inset = (collectionView.bounds.size.width - ((cellCount-1) * (cellWidth + cellSpacing))) * 0.5; 
    inset = MAX(inset, 0.0); 

    if(UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)){ 

     NSLog(@"Changed to landscape Spacing"); 
     return inset; 

    } 
    else{ 

     return inset; 
    } 

}*/ 
@end 
+0

は、カスタムクラスを作成し、UICollectionViewCell' 'の代わりに使用する必要があります。 – vaibhav

+0

'UICollectionViewCell'カスタムクラスにはラベルの詳細と他のオブジェクトが含まれていますか? –

+0

カスタムクラスを作成することによって、 'cell.yourLabel.text = @"ある値 "とそれ以前に宣言されている他のオブジェクトを参照することによって、uiコンポーネントを維持することができます。 – vaibhav

答えて

0

カスタムを使用する明確な例collectionViewCell

コードの下に表示さUICollectionViewCellの別のクラスのサブクラスを作成します。

.hファイル:

#import <UIKit/UIKit.h> 

@interface CollectionViewCell : UICollectionViewCell 
@property (weak, nonatomic) IBOutlet UILabel *customLabel; 
@end 

.mファイル:

#import "CollectionViewCell.h" 

@implementation CollectionViewCell 

@end 

今ドラッグ&ドロップをcollectionView内側viewControllerstoryboard次にセルセットカスタムクラスを選択し、ラベルのIBOutletを下記の画像に接続します。

enter image description here

絵コンテから注意ラベルや他のUIコンポーネントを追加する場合:ラベルのコンセントを接続

enter image description here

:カスタムクラスの設定

ドラッグをuilabel内部の細胞IBOutletを接続する前に


viewControllerクラス内でセルを設定します。 、dataSuorceIBOutletを接続してcollectionViewを正しく設定してください。あなたが使用する前にcellForItemAtIndexPathメソッド内で与えられているMyCustomCell

#import "ViewController.h" 
#import "CollectionViewCell.h" 

@interface ViewController(){ 
    // instance variable deceleration part 
    NSMutableArray *yourArray; 
} 
@end 

@implementation ViewController 

- (void)viewDidLoad{ 
    [super viewDidLoad]; 
    _yourCollView.delegate = self; 
    _yourCollView.dataSource = self; 

    yourArray = [[NSMutableArray alloc] initWithObjects:@"1st cell",@"2nd cell",@"3rd cell",@"4th cell", nil]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

// collection view delegate methods 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 
    return [yourArray count]; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 

    CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"MyCustomCell" forIndexPath:indexPath]; 

    // configuring cell 
    // cell.customLabel.text = [yourArray objectAtIndex:indexPath.row]; // comment this line if you do not want add label from storyboard 

    // if you need to add label and other ui component programmatically 
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height)]; 
    label.tag = 200; 
    label.text = [yourArray objectAtIndex:indexPath.row]; 

    // this adds the label inside cell 
    [cell.contentView addSubview:label]; 

    return cell; 
} 

//Note: Above two "numberOfItemsInSection" & "cellForItemAtIndexPath" methods are required. 

// this method overrides the changes you have made to inc or dec the size of cell using storyboard. 
- (CGSize)collectionView:(UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    return CGSizeMake(100, 100); 
} 

} // class ends 

セットアップ(セルを選択することによって)セル識別子は、画像の下に参照してください。

enter image description here

注:白にuilabelのテキストの色を変更します以前はデフォルトでcollectionViewが黒く表示されています。

こちらをご理解ください。

+0

新しい 'UICollectionViewCell'を作成しようとすると重複エラーが発生します。私も 'UILabel'を.hファイルに追加することはできません。 –

+0

私はいくつかの時間を与えましょう。私はできるだけ早く答えを更新します。 – vaibhav

+1

ありがとう、ありがたいです。 –

1
  1. UICollectionViewCellのサブクラスを作成します。例えば、TestCollectionViewCell
  2. Storyboardセル内にlabelをドラッグし、このUICollectionViewCell"Custom class"を作成したクラスで設定します。 のコレクションビューでDataSourceDelegateを設定することを忘れないでください。collectionViewを設定すると、Reusable identifierに設定されます。
  3. IBOutletをCellサブクラスに接続します。
  4. numberOfItemsInSectionメソッド内に少なくとも1つの値を設定します。
  5. cellForItemAtにセルのサブクラスを使用し、ラベルにテキストを設定してみます。
+0

データを収集する第三者のフレームワークの性質のためにプログラムで作成する必要があります。だから私はストーリーボードを使用することはできません –

+0

awakeFromNibでIBOutletsとinitビューなしで同じことをします –

0

あなたが不足している:

[cell.contentView addSubview:issue]; 
関連する問題