【问题标题】:XCode 6 UICollectionview viewwithtag not workingXCode 6 UICollectionview viewwithtag 不起作用
【发布时间】:2014-09-21 18:49:19
【问题描述】:

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

我在带有情节提要的 XCode 6 中使用以下代码。 创建了 50 个单元格,但标签不可见。一切都设置正确,如标签等。 如果我使用“旧的”XCode 5 方式对 Register cell 进行分类,它似乎适用于 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

【问题讨论】:

  • 如果单元格是在情节提要中制作的(适用于 iOS 7 或 8),则不应注册该类。如果您注册课程,您将获得默认的基本单元格。
  • 谢谢。我现在没有注册这个类,但是iOS 8下的问题是在加载collectionview时标签没有随内容更新。当我开始滚动时,单元格开始随着内容更新。
  • @Ben,你找到解决方案了吗?
  • 嗨。我找到了解决方案,就像我以前那样做。删除用于注册单元格的行并将重用标识符放在您处理单元格的位置。很抱歉我对此的回复晚了。
  • 我认为这与@rdelmar 首先提出的建议相同。

标签: ios uicollectionview xcode6 uicollectionviewcell viewwithtag


【解决方案1】:

我找到了解决方案,就像我以前那样做。删除用于注册单元格的行并将重用标识符放在您处理单元格的位置。抱歉,我对此的回复迟了

【讨论】:

  • 是的,我有同样的问题,我想我在哪里以编程方式注册了单元格,但也在 Interface Builder 中设置了单元格名称,这破坏了事情。删除了程序化的,一切都很好。
  • 我也遇到了同样的问题,这个解决方案对我有用 - 我不知道为什么会这样,因为 registerForClass 选择器包含在自动生成的代码中。
【解决方案2】:

我在使用 Xcode 6.3 beta 和 IOS 8 时遇到了类似的问题。我是这样解决的。首先选择视图并禁用 Storyboard 中的 Size Classes。 然后清理并构建应用程序。之后启用 Size Classes 。它现在可以工作了。

【讨论】:

  • 您是否为此提交了雷达文件?这在 Xcode 6.3.2 中仍然是一个问题,我很想欺骗它。另外,您是否测试过这是否是 Xcode 7 中的问题?
猜你喜欢
  • 2014-11-06
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-18
  • 2014-08-29
  • 2014-07-31
  • 2014-11-11
相关资源
最近更新 更多