Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XCode 6 UICollectionview viewwithtag not working

Seems XCode 6 is different in using viewwithtags then XCode 5 was.

I am using the following code in XCode 6 with Storyboards. 50 cells are created but the label is not visible.Everything is set correctly like the tag etc. If I use the "old" XCode 5 way to do it with Register cell classed it seems to work for iOS 7 but in iOS 8 the data is not passed to the label until I start to scroll.

static NSString * const reuseIdentifier = @"MyCell";

- (void)viewDidLoad {
    [super viewDidLoad];

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

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 50;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    UILabel *nameLabel = (UILabel *)[cell viewWithTag:102];    
    nameLabel.text = @"Hello World";
    nameLabel.textColor = [UIColor whiteColor];

    cell.backgroundColor = [UIColor blueColor];

    return cell;
}

Updated answer as this works but under iOS 8 the first cell just wont display the content. Only after scrolling up and down the content is loading to the label.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    UILabel *nameLabel = (UILabel *)[cell viewWithTag:102];    
    nameLabel.text = @"Hello World";
    nameLabel.textColor = [UIColor whiteColor];

    cell.backgroundColor = [UIColor blueColor];

    return cell;
}

Screenshots here:

https://www.dropbox.com/s/xs6gbvz3d04e4hv/Bildschirmfoto%202014-09-21%20um%2023.08.18.png?dl=0 https://www.dropbox.com/s/tp67rznggi5pcwt/Bildschirmfoto%202014-09-21%20um%2023.10.06.png?dl=0

like image 670
Ben Avatar asked Sep 21 '14 18:09

Ben


1 Answers

I found the solution in doing it the way like I did before. Remove the line to register the cell and put the reuseIdentifier where you handle the cell. Sorry for my late response on this

like image 128
Ben Avatar answered Nov 14 '22 23:11

Ben