Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel in custom UICollectionViewCell always null, can't update text

I created a collection view very simple to an Apple collection view sample project. I have a collection view in a view controller in storyboard, and set a label inside the collection view cell in the top right part of the collection view. I've hooked that up to the IBOutlet in my custom cell. Here's the relevant code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.workoutView registerClass:[Cell class] forCellWithReuseIdentifier:@"Cell"];
    ...
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.collView) {
        Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];    
        cell.segmentTitle.text = @"some text";
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    }
    return nil;
}

I put a breakpoint after the segmentTitle.text part and segmentTitle is always null. Accordingly what I see in the simulator is empty white boxes. What did I miss?

like image 678
brodney Avatar asked Apr 05 '13 00:04

brodney


2 Answers

UICollectionViewCell inside StoryBoard don't need to registerClass, just choose reuseidentifier in StoryBoard. Delete this line:

   // [self.workoutView registerClass:[Cell class] forCellWithReuseIdentifier:@"Cell"];

And make sure you connect right way:

-Select class type of UICollectionViewCell in storyBoard to Cell

-Drag UILabel into Cell and hook up to Cell.h

-Type the reuse identifier

like image 134
LE SANG Avatar answered Oct 18 '22 23:10

LE SANG


MainFeedCollectionView.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")

I removed this line from my code now its now working fine... 

like image 38
Balagurubaran Avatar answered Oct 19 '22 00:10

Balagurubaran