如何以编程方式在uicollectionview中插入图像? [英] How to insert images in uicollectionview programmatically?
问题描述
我对Objective-C很新,所以希望这一切都有道理。我在第一个答案中提供了代码以编程方式创建UICollectionView 。它工作正常。现在我想在单元格中添加一些可以通过鼠标点击扩展的图片。我搜索了许多教程,但都使用了nib文件或故事板文件。我怎么能以编程方式完成此任务?
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 ?
任何帮助将不胜感激。在此先感谢。
Any help would be greatly appreciated. Thanks in advance.
推荐答案
初学者阅读tutroial并首先了解以下链接和苹果文档
Beginner read tutroial and understand first everyting in below link and apple doc
ios-programming-uicollectionview-tutorial-with-sample-代码
像这种方法改变你的背景颜色
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;
}
输出:
这篇关于如何以编程方式在uicollectionview中插入图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!