XCode 6 UICollectionview viewwithtag not working

匿名 (未验证) 提交于 2019-12-03 03:10:03

问题:

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

回答1:

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



回答2:

I had similar problem when I was using Xcode 6.3 beta and IOS 8. I solved it like this. First select view and disable the Size Classes in Storyboard. Then clean and build the app. After that enable the Size Classes . It will work now.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!