2016-04-02 18 views
0

UICollectionView ve UICollectionViewCells ile ilk kez çalışıyorum. Basit bir görünüm oluşturmaya çalışıyorum. Karşı karşıya olduğum sorunlar şunlardır: toplama görünümü verileri yeniden yükleme, metin (dizi) hücre etiketlerine görüntüleniyor .. kodu burada .. yapabildiğiniz kadar yardım edin .. < 3 (eklediğimde uygulama çöküyor subview)CollectionView ve collectionViewCells ile çalışma

#import "MainCollectionViewController.h" 
#import "MainCollectionViewCell.h" 


@interface MainCollectionViewController() <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout> 

{ 
    UICollectionView *colView; 
} 
@end 

@implementation MainCollectionViewController 

static NSString * const reuseIdentifier = @"Cell"; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 


// [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier]; 


    self.actressArray = [NSMutableArray arrayWithObjects:@"John", @"Patrick", @"Jack", @"Clara", @"Nicol", @"Amanda", @"Nick", @"Bob", @"Jeff", @"Jerremy", @"Gloria", @"Bred", @"Seth", @"Peter", @"Lous", nil]; 



    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init]; 
    colView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout]; 
    [colView setDataSource:self]; //დელეგატების კოდით ჩართვა 
    [colView setDelegate:self]; 

    [colView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier]; 
    [colView setBackgroundColor:[UIColor grayColor]]; //ვიუს ბექგრაუნდის ფერი 

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; 
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical]; 
    [self.collectionView setCollectionViewLayout:flowLayout]; 

    [self.view addSubview:colView]; //ვიუზე ჩვენ მიერ გამზადებული ვიუს დაგდება 

    [self.collectionView registerClass:[MainCollectionViewCell class] forCellWithReuseIdentifier: reuseIdentifier]; 


    ////[self.collectionView reloadData]; 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 




//// In a storyboard-based application, you will often want to do a little preparation before navigation 
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender //{ 
//} 




- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { 

    return _actressArray.count; 
} 


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


    return 3; 
} 

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

    MainCollectionViewCell *cell = (MainCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 



    NSString *actressStr = [_actressArray objectAtIndex:indexPath.row]; 
    cell.actressLabel.text = actressStr; 

    //[self.collectionView reloadData]; 

    //[cell.contentView addSubview: ]; 



    return cell; 
} 

cevap

0

kez daha You Outlet Bağlantısını kontrol edin: -

Ve Ne Var Hatası ???

+0

çalışır, ancak yenisini zorladı. cellForItemAtIndexPath içinde – Marie

+0

cellForItemAtIndexPath çalışma verileri yeniden yüklemek için viewDidLoad viewWillAppear denedim collectionview –

+0

yeniden yüklemeye gerek yok ama çalışmıyor – Marie

0

buradan güncellenmiştir. yeniden veri yok ben bu sorunu çözmüş

#import "MainCollectionViewController.h" 
#import "MainCollectionViewCell.h" 


@interface MainCollectionViewController() <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout> 

{ 
    UICollectionView *colView; 
} 
@end 

@implementation MainCollectionViewController 

static NSString * const reuseIdentifier = @"Cell"; 





- (void)viewDidLoad { 
    [super viewDidLoad]; 


// [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier]; 


    self.actressArray = [NSMutableArray arrayWithObjects:@"John", @"Patrick", @"Jack", @"Clara", @"Nicol", @"Amanda", @"Nick", @"Bob", @"Jeff", @"Jerremy", @"Gloria", @"Bred", @"Seth", @"Peter", @"Lous", nil]; 



    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init]; 
    colView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout]; 
    [colView setDataSource:self]; //დელეგატების კოდით ჩართვა 
    [colView setDelegate:self]; 

    [colView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier]; 
    [colView setBackgroundColor:[UIColor grayColor]]; //ვიუს ბექგრაუნდის ფერი 

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; 
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical]; 
    [self.collectionView setCollectionViewLayout:flowLayout]; 

    [self.view addSubview:colView]; //ვიუზე ჩვენ მიერ გამზადებული ვიუს დაგდება 

    [self.collectionView registerClass:[MainCollectionViewCell class] forCellWithReuseIdentifier: reuseIdentifier]; 

    [colView reloadData]; 

} 



//// In a storyboard-based application, you will often want to do a little preparation before navigation 
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
//} 



- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { 

    return 2; 
} 


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


    return _actressArray.count; 
} 

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



    MainCollectionViewCell *cell = (MainCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 

    cell.backgroundColor = [UIColor whiteColor]; 
    cell.layer.cornerRadius = 10; 
    cell.layer.borderWidth = 2.0; 
    cell.layer.borderColor = [[UIColor lightGrayColor] CGColor]; 

    UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, cell.bounds.size.width -20 , cell.bounds.size.height)]; 
    testLabel.text = _actressArray[indexPath.row]; 
    testLabel.font = [UIFont fontWithName:@"Helvetica" size:12]; 
    testLabel.textAlignment = NSTextAlignmentCenter; 
    testLabel.lineBreakMode = NSLineBreakByWordWrapping; 
    testLabel.numberOfLines = 0; 

    [cell.contentView addSubview:testLabel]; 

    return cell; 
} 



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return CGSizeMake(collectionView.frame.size.width/3.2 , 100); 
} //ხაზზე სამისთვის 
+0

başlatıcılar için UICollectionViewController. Ayrıca, viewDidLoad() öğesinde olduğunda reloadData'ye gerek kalmaz, reloadData yalnızca verileriniz değiştiğinde kullanılır. –

İlgili konular