简体   繁体   English

UICollectionViewCell有时不显示

[英]UICollectionViewCell sometimes not showing up

I'm trying to get collection view cells with a background image and text on them. 我正在尝试获取具有背景图像和文本的集合视图单元格。 When I build and run it sometimes they show up in the simulator and sometimes they don't even when I don't change the code. 当我构建并运行它时,有时它们会出现在模拟器中,有时即使我不更改代码也不会出现。

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat frameWidth = screenRect.size.width - 40;
    CGFloat frameHeight = screenRect.size.height - 20;

    _collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(20, 20, frameWidth, frameHeight) collectionViewLayout:layout];
    [_collectionView setDataSource: self];
    [_collectionView setDelegate: self];

    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    [_collectionView setBackgroundColor:[UIColor clearColor]];

    [self.view addSubview:_collectionView];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.articles count];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    UILabel *articleTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 40)];
    NSDictionary *article = self.articles[indexPath.row];


    NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: article[@"image"]]];

    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:imageData]];

    articleTitle.text = article[@"title"];
    articleTitle.textColor = [UIColor whiteColor];
    [cell.contentView addSubview:articleTitle];


    return cell;
}

"articles" is a json object with title, image and url keys. “文章”是带有标题,图像和url键的json对象。 They seem to work when the cells show up but as I said they don't always show. 当细胞出现时它们似乎起作用,但正如我所说,它们并不总是出现。

This is my first app so any help would be really appreciated. 这是我的第一个应用程序,因此任何帮助将不胜感激。

The numbers of section will be depend on article count. 部分的数量将取决于文章数。 Try placing breakpoint in there, your article array might be empty, if so collection view data source will not get any data to be displayed. 尝试在其中放置断点,您的文章数组可能为空,如果这样,则集合视图数据源将不会显示任何数据。 Or if self.articel refers to an array then try replacing line NSDictionary *article = self.articles[indexPath.row]; 或者,如果self.articel指向数组,则尝试替换NSDictionary行* article = self.articles [indexPath.row]; With NSDictionary *article = [self.articles objectatIndexPath:indexPath.row]; 使用NSDictionary * article = [self.articles objectatIndexPath:indexPath.row];

Or post your full code so we could see for other possible bug. 或发布您的完整代码,以便我们发现其他可能的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM