Dan*_*iel 6 objective-c ios uicollectionview uicollectionviewcell
刚开始UICollectionView.我用IB创建了一个简单UICollectionView的UIViewController.它通过分页水平滚动.我在UICollectionViewCell里面放了一个简单的UICollectionView.我为单元格设置了重用标识符.
我在UILabel里面放了一个100的标签UICollectionViewCell.
在viewDidLoad,我打电话:
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Thing"];
Run Code Online (Sandbox Code Playgroud)
后来我试图像这样初始化单元格:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Thing" forIndexPath:indexPath];
cell.backgroundColor = [UIColor greenColor];
UILabel *nameLabel = (UILabel *)[cell viewWithTag:100];
nameLabel.text = @"Bogus";
return cell;
}
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,视图正确加载; 我可以水平滚动通过2个单元格.我看到丑陋的绿色标识每个细胞.
但是,该viewWithTag方法返回nil,因此未设置nameLabel文本.
为什么?
请注意,我还尝试UICollectionViewCell使用它定义自定义子类并调用registerClass.在IB中,我将单元类更改为自定义子类,并将其绑定UILabel到UILabel子类中的插座.
但这也行不通.(我宁愿避免使用自定义子类方法,因为似乎没有必要定义只是为了保持类IBOutlets.)
我在想我错过了一些明显的东西.
这里描述的类似问题:
| 归档时间: |
|
| 查看次数: |
5186 次 |
| 最近记录: |