似乎 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/