点击Uitableview行上的不同图像

时间:2012-09-26 06:50:57

标签: iphone ios sqlite uiimageview uiimage

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

数据库中的第一张图片,

 -(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;

}

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

2 个答案:

答案 0 :(得分:0)

在推送视图控制器之前添加数据:

- (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];

}

答案 1 :(得分:0)

您应该在设置数据后将detailViewController推送到其对象。因为detailViewController会在获取任何内容之前导航,因此所有对象都将保持原样相同。

并在[self Readsqlitefile];方法中致电didSelectRowAtIndexPath:,它会更新您的mysign

但请记住,如果您只是sqlite进行练习,那么UIImage中的sqlite其他明智的保存UIImage并不是一个好习惯,您的应用可能会遭到拒绝。切勿将{{1}}保存在数据库中。