ios xcode如何在数据库中的集合视图单元格中显示图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15150721/
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
xcode how to display images in collection view cell from database
提问by Hasintha Janka
I am just a beginner. i want to display image which is stored in database in a collection view cell. I have already created database and collection view. my code is as follow,
我只是一个初学者。我想在集合视图单元格中显示存储在数据库中的图像。我已经创建了数据库和集合视图。我的代码如下,
MyDatabase *data;
data=[MyDatabase new];
imagearray=[data OpenMyDatabase:@"SELECT pic_name FROM exterior" :@"pic_name"];
so my question is how can i display images ? let me know the way/code thanks in advance
所以我的问题是如何显示图像?让我知道方式/代码提前谢谢
回答by Hasintha Janka
You can use following code for display UIImage
grid. Add UICollectionView
for your xib
file. Don't forget to set the delegate
in collection.
您可以使用以下代码显示UIImage
网格。UICollectionView
为您的xib
文件添加。不要忘记设置delegate
in 集合。
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return noOfItem/ noOfSection;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return noOfSection;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell =
[collectionView dequeueReusableCellWithReuseIdentifier:identifier
forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [imageArray objectAtIndex:
(indexPath.section * noOfSection + indexPath.row)];
return cell;
}
In your xib file add UIImageView to your CollectionViewCelland change it tagvalue to 100.
在您的 xib 文件中,将 UIImageView 添加到您的CollectionViewCell并将其标记值更改为 100。
回答by Swapnil
Please go through below links.
请通过以下链接。
Above link is from Apple developer site. It has all details of UIcollectionview and tutorials related it. Below URL is also having sample tutorial, which will help you a lot.
以上链接来自苹果开发者网站。它包含 UIcollectionview 的所有详细信息以及与之相关的教程。下面的 URL 也有示例教程,这将对您有很大帮助。
http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12
http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12
You have images array retrieved from database. Now it will act as data source for collection view. You need to form UICollectionViewCell which will have those images accordingly. Please study above links, you will get to know.
您有从数据库中检索到的图像数组。现在它将作为集合视图的数据源。您需要形成 UICollectionViewCell ,它将相应地包含这些图像。请研究上面的链接,你会知道的。