【问题标题】:UITableview images from Sqlite database来自 Sqlite 数据库的 UITableview 图像
【发布时间】:2016-01-08 19:27:10
【问题描述】:

我想在 SQLite 的 tableView 中显示图像。这是我从 SQLite 代码中检索图像代码。如何在 tableview 中加载图像?

-(void) retrieveData{
sqlite3 * database;
NSMutableArray *prodnamemutable = [[NSMutableArray alloc] init];
NSMutableArray *prodpricemutable = [[NSMutableArray alloc] init];
NSMutableArray *prodimagemutable = [[NSMutableArray alloc] init];
NSString *databasename=@"contacts.sqlite";  // Your database Name.

NSArray * documentpath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES);

NSString * DocDir=[documentpath objectAtIndex:0];

NSString * databasepath=[DocDir stringByAppendingPathComponent:databasename];

if(sqlite3_open([databasepath UTF8String], &database) == SQLITE_OK)
{
    const char *sqlStatement = "SELECT * FROM contacts";  // Your Tablename

    sqlite3_stmt *compiledStatement;

    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
    {
        [prodnamemutable removeAllObjects];

        while(sqlite3_step(compiledStatement) == SQLITE_ROW)
        {

            [prodnamemutable addObject:[NSString stringWithFormat:@"%s",(char *) sqlite3_column_text(compiledStatement, 1)]];
            [prodpricemutable addObject:[NSString stringWithFormat:@"%s",(char *) sqlite3_column_text(compiledStatement, 3)]];

            int length = sqlite3_column_bytes(compiledStatement, 0);
            NSData *imageData = [NSData dataWithBytes:sqlite3_column_blob(compiledStatement, 0) length:length];
            [prodimagemutable addObject:imageData];
            NSLog(@"Length from db : %lu", (unsigned long)[imageData length]);
            imageArray = [NSArray arrayWithArray:prodimagemutable];
            NSLog(@"image found.%@",imageArray);
        }
    }
    nameArray = [[NSArray alloc]initWithArray:prodnamemutable];
    priceArray = [[NSArray alloc]initWithArray:prodpricemutable];
    NSLog(@"nameArray:%@",nameArray);
    [[self navigationController] tabBarItem].badgeValue = [NSString stringWithFormat:@"%lu",(unsigned long)[nameArray count]];
    sqlite3_finalize(compiledStatement);
    [self.tableView reloadData];

}
sqlite3_close(database);

}

【问题讨论】:

    标签: ios objective-c sqlite uitableview


    【解决方案1】:
    UIImage *image = [UIImage imageWithData:data];
    

    这会将您的图像NSData 转换为UIImage 现在此图像可以显示在任何UIImageView 上。

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;    //count of section
    }
    
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
        return [prodimagemutable count];    //count number of row from image array.
    }
    
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView
                 cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
    
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
    
            if (cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                               reuseIdentifier:MyIdentifier];
            }
            cell.imageView.image = [UIImage imageWithData:[prodimagemutable objectAtIndex: indexpath.row]] ;
            return cell;
        }
    //Below use for set height of cell
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
            return 80;// or the height you want.
    
    }
    

    【讨论】:

    • 在你的VC中确认UITableViewDataSource和UITableViewDelegate。
    • 但是第一张图片重复了第二行,第二行图片显示在第三行,如何解决。
    • 如果我在表上添加新项目,它会显示第一行图像。请给一些建议。
    • 图像重复可能是您的 prodimage 数组的结果,您是否检查过它是否有不同的图像。
    • 对于第二个问题,您能否详细说明实际发生的情况。
    【解决方案2】:

    如果你把代码分成多个类会更好,如下所示

    1.数据库管理器

    @interface DBManager : NSObject
    - (NSArray *)getImages;
    @end
    
    @implementation DBManager
    - (NSArray *)getImages {
        // put your code to retrieve the images from the database
        // use the following to convert from NSData to UIImage
        UIImage *image = [UIImage imageWithData:imageData];
    }
    @end
    

    2。 ImagesTableViewController

    @interface ImagesTableViewController : UITableViewController
    @property(nonatomic,strong) NSArray* images;
    @end
    
    
    @implementation ImagesTableViewController
    - (void)viewDidLoad {
        self.images = [dbManager getImages];
    }
    #pragma mark - Table view data source
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return self.images.count;
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
        }
        cell.imageView.image = self.images[indexpath.row];
        return cell;
    }
    
    @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-23
      • 2015-05-25
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      相关资源
      最近更新 更多