ios - XCode 6 UICollectionview viewwithtag 不工作

标签 ios uicollectionview xcode6 uicollectionviewcell viewwithtag

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

我在带有 Storyboard的 XCode 6 中使用以下代码。 创建了 50 个单元格,但标签不可见。一切都已正确设置,如标签等。 如果我使用“旧的”XCode 5 方法通过 Register cell classed 来做到这一点,它似乎适用于 iOS 7,但在 iOS 8 中,数据不会传递到标签,直到我开始滚动。

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 下,第一个单元格不会显示内容。只有在向上和向下滚动后,内容才会加载到标签。

- (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 放在处理单元格的位置。抱歉,我对此的回复晚了

关于ios - XCode 6 UICollectionview viewwithtag 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25962671/

相关文章:

ios - View Controller 在执行 segue 时不释放

ios - 如何在 Collection View 单元格中设置多个图像?

ios - Collection View Controller 单元格间距

xcode - 如何更改 ImageView 的图像?

ios - XCode代码签名错误XCTest

ios - 从其他 UIViewController 更改 UIViewController 的背景颜色

ios - 从AIFF转换的AVAudioPlayer M4A文件产生的延迟不同于从ADTS AAC转换的M4A

ios - NavigationBar 后退按钮在 popViewController 之后更改文本

ios - 获取当前界面方向的不同方式?

用于复杂设计和架构的 IOS 静态库