简体   繁体   English

xcode如何在数据库的集合视图单元格中显示图像

[英]xcode how to display images in collection view cell from database

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 提前告知我方式/代码

You can use following code for display UIImage grid. 您可以使用以下代码来显示UIImage网格。 Add UICollectionView for your xib file. xib文件添加UICollectionView Don't forget to set the delegate in collection. 不要忘记在集合中设置delegate

- (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 CollectionViewCell and change it tag value to 100. 在xib文件中,将UIImageView添加到CollectionViewCell并将其标记值更改为100。

Please go through below links. 请通过下面的链接。

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

Above link is from Apple developer site. 上面的链接来自Apple开发人员网站。 It has all details of UIcollectionview and tutorials related it. 它具有UIcollectionview的所有详细信息以及与之相关的教程。 Below URL is also having sample tutorial, which will help you a lot. 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. 您需要形成UICollectionViewCell,它将具有相应的图像。 Please study above links, you will get to know. 请研究以上链接,您将了解。

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

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