XCode 6 UICollectionview viewwithtag不起作用 [英] XCode 6 UICollectionview viewwithtag not working

查看:123
本文介绍了XCode 6 UICollectionview viewwithtag不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎XCode 6使用viewwithtags与XCode 5不同。

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

我在XCode 6中使用以下代码与Storyboard。
创建了50个单元格,但标签不可见。所有内容都设置正确,如标签等。
如果我使用旧XCode 5方式进行注册单元格分类它似乎适用于iOS 7但在iOS 8中,在我开始滚动之前,数据不会传递给标签。

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;
}

更新后的答案,但在iOS 8下,第一个单元格不会显示内容。只有在向上和向下滚动后,内容才会加载到标签。

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;
}

屏幕截图:

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

推荐答案

我找到了解决方案,就像我之前一样。删除行以注册单元格,并将reuseIdentifier放在处理单元格的位置。很抱歉我迟到了对此

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

这篇关于XCode 6 UICollectionview viewwithtag不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆