【问题标题】:How to insert images in uicollectionview programmatically?如何以编程方式在 uicollectionview 中插入图像?
【发布时间】:2014-02-17 06:55:55
【问题描述】:

我对 Objective-C 很陌生,所以希望这一切都有意义..我运行了第一个答案中提供的代码 Creating a UICollectionView programmatically..它工作正常。现在我想在单元格中添加一些图片,可以扩展鼠标点击。我搜索了很多教程,但都使用 nib 文件或故事板文件。我如何以编程方式完成此任务?

任何帮助将不胜感激。提前致谢。

【问题讨论】:

  • 了解基础知识raywenderlich.com/22324/…
  • 作为初学者,您应该按照网络上的完整教程而不是 SO,然后来这里查询。

标签: ios iphone objective-c uicollectionview uicollectionviewcell


【解决方案1】:

正如洛根所说:

#define IMG_TAG 1000
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    cell.textLabel.text = @"test.....";
    UIImage * img = [UIImage imageNamed: @"sampleImage"];

    UIImageView * customImageView = (UIImageView*) [cell viewWithTag: IMG_TAG];
    // two cases:
    // 1) we got a "recycled" cell, so we already have an image view added
    // 2) we got a "new" cell, so we must create, add image view AND TAg
    // in both cases we will set image

    if (customImageView){
        // case 1
        // nothing special
    }else{
        // case 2:
        // add and tag:
        customImageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 2, 30, 30)];
        [cell addSubview: customImageView];
        customImageView.tag = IMG_TAG;
    }

    customImageView.image = img;
    return cell;
}

请查看并点赞:)

【讨论】:

    【解决方案2】:

    初学者阅读教程并首先了解以下链接和苹果文档中的所有内容

    ios-programming-uicollectionview-tutorial-with-sample-code

    https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/Reference/Reference.html

    像这种方法一样改变你的背景颜色

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
        UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
        recipeImageView.image = [UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]];
        cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];
        [self.view addSubview:recipeImageView];
        cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]]];
    
        return cell;
    }
    

    输出:

    【讨论】:

    • 你的项目有什么问题
    • 是的,我已经通过放置图像并声明类似的数组来放置你的函数,就像在教程中一样......但它没有在单元格中显示图像......我认为它们是连接所需的某些命令带有文件所有者的 imageviewer 和 collection-cell ...就像我们在情节提要中手动执行的操作一样...
    • 如果可能的话链接你的项目
    • 你可以从这个链接下载项目:skydrive.live.com/…
    • 因为您正在调用[cell viewWithTag:100],所以我假设您的视图已作为子视图添加到单元格中。这是不正确的。不应该直接将子视图添加到单元格,而是添加到单元格的 contentView。即[cell.contentView addSubview:imageView]; & [cell.contentView viewWithTag:100];
    【解决方案3】:

    注意:

    上面的代码是错误的!

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
        UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
        recipeImageView.image = [UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]];
        cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];
        [self.view addSubview:recipeImageView];
        cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]]];
    
        return cell;
    }
    

    您分配(最好分配一次无名标签...)但每次调用 cellForItemAtIndexPath 时代码都会添加子视图。

    【讨论】:

    • 我认为最好显示正确的答案而不是重述错误的代码。我想他们想要didSelectCellAtIndex
    猜你喜欢
    • 2019-08-23
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多