0

我的应用程序中有tableview,因为当我单击该行时,视图打开,包含两个图像查看器,其中两个图像从数据库中形成两个不同的表,一个显示图像的图像查看器取决于单击的行,其中第二个为所有行显示相同的图像。这是我的代码,

来自数据库的第一张图片,

 -(void)Readthesqlitefileforname:(NSString *)brandname
 {  
    sqlite3 *database;//database object
NSString *docpath=[self doccumentspath];
const char *ch=[docpath UTF8String];//string to constant char UTF8string  main part to connect DB

if (sqlite3_open(ch, &database)==SQLITE_OK) 
   {

   NSString *strstmt=[NSString stringWithFormat:@"select name,nik,dob,study,phone,mail,fsong,dp from scrap where name = '%@'",brandname];

    const char *chstmt=[strstmt UTF8String];

    sqlite3_stmt *sqlstmt;//to execute the above statement
    if (sqlite3_prepare_v2(database, chstmt, -1, &sqlstmt, NULL)==SQLITE_OK)
    {
        while (sqlite3_step(sqlstmt)==SQLITE_ROW) {

            const char *ch=(char *)sqlite3_column_text(sqlstmt, 0);
            bnnam=[NSString stringWithFormat:@"%s",ch];
            const char *ch1=(char *)sqlite3_column_text(sqlstmt, 1);
            urladdr=[NSString stringWithFormat:@"%s",ch1];
            const char *ch2=(char *)sqlite3_column_text(sqlstmt, 2);
            ydob=[NSString stringWithFormat:@"%s",ch2];
            const char *ch3=(char *)sqlite3_column_text(sqlstmt, 3);
            ystud=[NSString stringWithFormat:@"%s",ch3];
            const char *ch4=(char *)sqlite3_column_text(sqlstmt, 4);
            yphon=[NSString stringWithFormat:@"%s",ch4];
            const char *ch5=(char *)sqlite3_column_text(sqlstmt, 5);
            ymail=[NSString stringWithFormat:@"%s",ch5];
            const char *ch6=(char *)sqlite3_column_text(sqlstmt,6);
            yface=[NSString stringWithFormat:@"%s",ch6];                //NSLog(@"%@",urladdr);
            //const char *ch5=(char *)sqlite3_column_text(sqlstmt, 4);
            NSUInteger legnth=sqlite3_column_bytes(sqlstmt, 7);

            if (legnth>0) 
            {

                NSData *dt=[NSData dataWithBytes:sqlite3_column_blob(sqlstmt, 7) length:legnth];
                clsimg=[UIImage imageWithData:dt];//converting data to image
                NSLog(@"image 1 %@",clsimg);
            }
            else 
            {
                clsimg=nil;
            }

        }
    }
    sqlite3_finalize(sqlstmt);
}
sqlite3_close(database);
 } 

来自数据库的第二张图片,

  -(void)Readsqlitefile
 {
sqlite3 *database;//database object
NSString *docpath=[self doccumentspath];//get sqlite path
const char *ch=[docpath UTF8String];
if (sqlite3_open(ch, &database)==SQLITE_OK) 
    {
const char *chstmt="select * from scrapsign";
sqlite3_stmt *sqlstmt;
    if (sqlite3_prepare_v2(database, chstmt, -1, &sqlstmt, NULL)==SQLITE_OK)
    {
        while (sqlite3_step(sqlstmt)==SQLITE_ROW)
        {

            NSUInteger legnt=sqlite3_column_bytes(sqlstmt, 0);

            if (legnt>0) {

                NSData *dt=[NSData dataWithBytes:sqlite3_column_blob(sqlstmt, 0) length:legnt];
                mysign=[UIImage imageWithData:dt];
            }
            else 
                            {
                mysign=nil;
            }

        }
    }
    sqlite3_finalize(sqlstmt);
}
sqlite3_close(database);
 }

行点击的图像设置

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{

 scrapview *detailViewController = [[scrapview alloc] initWithNibName:@"scrapview" bundle:nil];
 [self Readthesqlitefileforname:[ar objectAtIndex:indexPath.row]];
 [self.navigationController pushViewController:detailViewController animated:YES];
 if (clsimg !=nil)
 {
    detailViewController.imgv.image=clsimg;

 }
 [self Readsqlitefile];

if (mysign !=nil) 
{
    detailViewController.imgv2.image=mysign;//second image viewer
    NSLog(@"%@",mysign);

}

detailViewController.txt1.text=urladdr;
detailViewController.txt2.text=ydob;
detailViewController.txt3.text=ystud;
detailViewController.txt4.text=yphon;
detailViewController.txt5.text=ymail;
detailViewController.txt.text=yface;
detailViewController.txt7.text=bnnam;

}

单击该行时,视图应在两个图像查看器上显示不同的图像,但只有一个图像视图仅在单击时更改,第二个图像视图为所有行单击显示相同的图像。请帮我解决问题。

4

2 回答 2

0

add your data before pushing view controller:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{

 scrapview *detailViewController = [[scrapview alloc] initWithNibName:@"scrapview" bundle:nil];
 [self Readthesqlitefileforname:[ar objectAtIndex:indexPath.row]];

 if (clsimg !=nil)
 {
    detailViewController.imgv.image=clsimg;

 }

 if (mysign !=nil) 
 {
    detailViewController.imgv2.image=mysign;//second image viewer
    NSLog(@"%@",mysign);    
 }

 detailViewController.txt1.text=urladdr;
 detailViewController.txt2.text=ydob;
 detailViewController.txt3.text=ystud;
 detailViewController.txt4.text=yphon;
 detailViewController.txt5.text=ymail;
 detailViewController.txt.text=yface;
 detailViewController.txt7.text=bnnam;

 //now push view controller
 [self.navigationController pushViewController:detailViewController animated:YES];

}
于 2012-09-26T07:08:20.423 回答
0

您应该将detailViewController设置后的数据推送到其对象。因为detailViewController会在获得任何东西之前导航,所以所有对象都将保持原样。

并调用它将更新您[self Readsqlitefile];的.didSelectRowAtIndexPath:mysign

但是请记住,如果您只是在练习中进行sqlite其他明智的保存UIImagesqlite这不是一个好习惯,您的应用程序可能会被拒绝。永远不要保存UIImage在数据库中。

于 2012-09-26T07:23:56.453 回答