ios 如何以编程方式在 uicollectionview 中插入图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21822616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:27:44  来源:igfitidea点击:

How to insert images in uicollectionview programmatically?

iosiphoneobjective-cuicollectionviewuicollectionviewcell

提问by user3239274

I'm pretty new to Objective-C so hopefully this all makes sense..I ran code provided in first answer Creating a UICollectionView programmatically..It is working fine .Now i want to add some pictures in cell that can expanded by mouse click .I searched many tutorial but all using nib files or storyboard files .How i can accomplish this task programmatically ?

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

Any help would be greatly appreciated. Thanks in advance.

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

采纳答案by codercat

Beginner read tutroial and understand first everyting in below link and apple doc

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

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

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

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

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

change this your blackground color like this approach

像这种方法一样改变你的黑底色

- (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;
}

output:

输出:

enter image description here

在此处输入图片说明

回答by ingconti

as Logan suggest:

正如洛根建议的那样:

#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;
}

pls review and upvote :)

请和upvote :)

回答by ingconti

NOTE:

笔记:

code above is WRONG!

上面的代码是错误的!

- (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;
}

You allocate (better to allocate ONCE unsung TAGs...) but the code will ADD subview EVERY time cellForItemAtIndexPath is called.

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