1

私は電子メールの署名を作成しています。これまでに2つのビューを作成しました「ViewController」と「ListView」のコーディングは次のようになります

ViewController.h

 #import <UIKit/UIKit.h>
 #import "ListView.h"
 #import "sqlite3.h"

@interface ViewController:UIViewController<UITextViewDelegate,UIImagePickerControllerDelegate>
 {
UIImageView *imageView;
UIImage *signimage;
UIImagePickerController *imagePicker;
NSMutableArray *hello;
NSMutableArray *hellocontent;

ListView *listview;
UITextField *signaturename;
UITextView *textView; // UITextView *scontent;
 // IBOutlet UIScrollView *scrollview;
 // BOOL keyboardIsShown;
  //IBOutlet UITextField *recordTextField;

NSString   *databasePath;
sqlite3 *Dummy2;
 }
 @property(nonatomic,retain) IBOutlet UIImageView *imageView;
 @property(nonatomic,retain) UIImage *signimage;
 -(IBAction)btnLoadImage:(id) sender;
  -(IBAction)clearFields:(id)sender;
  -(IBAction)show:(id)sender;
 // @property (nonatomic,retain) UITextField *recordsTextField;

 //@property(nonatomic,retain) IBOutlet UIScrollView *scrollview;
   @property(nonatomic, retain) IBOutlet UITextField *signaturename;
   @property(nonatomic, retain) IBOutlet UITextView *textView;  // scontent

   -(IBAction) btnsave:(id) sender;
    -(IBAction) keyboard:(id) sender;
    -(IBAction)next:(id)sender;

  -(IBAction) bgtouch:(id) sender;
   // -(IBAction)show:(id)sender;
   // -(IBAction)records:(id)s;
   // -(IBAction)updateQuery:(id)sender;

   //Static methods.
   + (void) getInitialDataToDisplay:(NSString *)dbPath;
   + (void) finalizeStatements;

   //Instance methods.
   - (id) initWithPrimaryKey:(NSInteger)pk;
    - (void) saveAllData;


   @end

ViewController.m

#import "ViewController.h"
#import"ListView.h"

 @implementation ViewController
 @synthesize signaturename,imageView,textView,signimage; // recordsTextField


     // scrollview;
   -(IBAction)next:(id)sender{

    sqlite3_stmt    *statement;

    const char *dbpath = [databasePath UTF8String];
     hello = [[NSMutableArray alloc]init]; 
    //  hellocontent = [[NSMutableArray alloc]init];

    if (sqlite3_open(dbpath, &Dummy2) == SQLITE_OK)
   {
    NSString *updateSQL = @"SELECT * FROM PROFILE";

    const char *update_stmt = [updateSQL UTF8String];

    sqlite3_prepare_v2(Dummy2, update_stmt, -1, &statement, NULL);
    while(sqlite3_step(statement) == SQLITE_ROW) {
          [hello addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)]];
    //    [hellocontent addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)]];
    }
    sqlite3_finalize(statement);
    sqlite3_close(Dummy2);
}   
listview = [[ListView alloc]initWithNibName:@"ListView" bundle:nil];
listview.arr=[[NSMutableArray alloc]init];
 listview.arr = hello;

 [self.view addSubview:listview.view];

  }

 - (void)setCoffeeImage:(UIImage *)theCoffeeImage {

// self.isDirty = YES;
[signimage release];
signimage = [theCoffeeImage retain];
  }


  -(IBAction) btnsave:(id) sender
  {

if (([self.signaturename.text length] && [self.textView.text length]) != 0) {

  //  NSData *data = UIImagePNGRepresentation(self.signimage);      


    sqlite3_stmt    *statement;

    const char *dbpath = [databasePath UTF8String];
    hello = [[NSMutableArray alloc]init]; 

    if (sqlite3_open(dbpath, &Dummy2) == SQLITE_OK)
    {


        NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO profile (sname,scontent) VALUES (\"%@\", \"%@\")", signaturename.text,textView.text];

        const char *insert_stmt = [insertSQL UTF8String];                          
       //  sqlite3_bind_blob(statement, 3, [data bytes], [data length], NULL);
        sqlite3_prepare_v2(Dummy2, insert_stmt, -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            // status.text = @"Contact added";
            signaturename.text = @"";            //address.text = @"";
            textView.text = @"";
            // imageUrl.text=@"";

        } else {
            // status.text = @"Failed to add contact";
        }
        sqlite3_finalize(statement);
        sqlite3_close(Dummy2);
    }


       }   
else
    {  
    UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Warning" message:@"Signature Name and Content Field Should Not Be Empty" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show]; 


   }


    }
 -(IBAction)show:(id)sender{
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt    *statement;

if (sqlite3_open(dbpath, &Dummy2) == SQLITE_OK)
{
    NSString *querySQL = [NSString stringWithFormat: @"SELECT sname, scontent FROM profile WHERE sname=\"%@\"", signaturename.text];

    const char *query_stmt = [querySQL UTF8String];

    if (sqlite3_prepare_v2(Dummy2, query_stmt, -1, &statement, NULL) == SQLITE_OK)
    {
        if (sqlite3_step(statement) == SQLITE_ROW)
        {
            NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
            textView.text = addressField;


           // status.text = @"Match found";

            [addressField release];

        } else {
          //  status.text = @"Match not found";
           textView.text = @"";

        }
        sqlite3_finalize(statement);
    }
    sqlite3_close(Dummy2);
  }
  }


  -(IBAction)clearFields:(id)sender
   {
signaturename.text=@"";

textView.text=@"";
    NSLog(@"clear is working");


    }

    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    if([text isEqualToString:@"\n"]) {
    [textView resignFirstResponder];
    return NO;
    }

    return YES;
    }
  -(IBAction) bgtouch:(id) sender
    {
    [signaturename resignFirstResponder];
    // [textView resignFirstResponder];
    }

    -(IBAction) keyboard:(id)sender
     {
    [sender resignFirstResponder];
    // [self.signaturecontent resignFirstResponder];
    }
  - (void)didReceiveMemoryWarning
 {
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

 #pragma mark - View lifecycle

- (void)viewDidLoad
{
imagePicker =[[UIImagePickerController alloc]init];
[super viewDidLoad];

NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

docsDir = [dirPaths objectAtIndex:0];

// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"Dummy2.sqlite"]];

NSFileManager *filemgr = [NSFileManager defaultManager];

if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &Dummy2) == SQLITE_OK)
    {
        char *errMsg;
        const char *sql_stmt = "CREATE TABLE IF NOT EXISTS profile(ID INTEGER PRIMARY KEY AUTOINCREMENT, SNAME TEXT, SCONTENT TEXT)";

        if (sqlite3_exec(Dummy2, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
        {
           // status.text = @"Failed to create table";
        }

        sqlite3_close(Dummy2);

    } else {
        // status.text = @"Failed to open/create database";
    }
}

[filemgr release];
// Do any additional setup after loading the view, typically from a nib.
  }
  -(IBAction)btnLoadImage:(id)sender
   {
imagePicker.delegate =self;
imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;

// show the image picker
[self presentModalViewController:imagePicker animated:YES];

   }

   -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *image;
NSURL *mediaUrl;
mediaUrl =(NSURL *)[info valueForKey:UIImagePickerControllerMediaURL];

if(mediaUrl == nil){
    image = (UIImage *)[info valueForKey:UIImagePickerControllerEditedImage];
    if(mediaUrl == nil){
        // original image selected
        image =(UIImage *)[info valueForKey:UIImagePickerControllerOriginalImage];

        // display the image
        imageView.image = image;
    }
    else {
        // edited image picked 
        CGRect rect = [[info valueForKey:UIImagePickerControllerCropRect]CGRectValue];

        // display the image
        imageView.image = image;
    }
        }

     // hide the image picker
     [picker dismissModalViewControllerAnimated:YES];
   }
 -(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
// user did not select image; hide the image picker
[picker dismissModalViewControllerAnimated:YES];
  }
 - (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{

[super viewWillAppear:animated];
}

 - (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{

[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}

 - (BOOL)shouldAutorotateToInterfaceOrientation:UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

ListView.h

 #import <UIKit/UIKit.h>


 @interface ListView : UIViewController<UITableViewDataSource,UITableViewDelegate>
  {
 NSMutableArray *arr;

 }
@property(nonatomic,retain) NSMutableArray *arr;
-(IBAction)back:(id)sender;
@end

ListView.m

#import "ListView.h"


 @implementation ListView
 @synthesize arr;

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
 }
-(IBAction)back:(id)sender
{
[super.view removeFromSuperview];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//  return 6;

return [arr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSString *cellvalue = [arr objectAtIndex:indexPath.row];
cell.textLabel.text=cellvalue;
// Configure the cell.
return cell;
}

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

 }



- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
 }

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
 }

- (BOOL)shouldAutorotateToInterfaceOrientation:UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

 @end

データベース「Dummy2.sql」はこんな感じ

PROFILE = table
id = field = INTEGER PRIMARY KEY
sname = field = TEXT
scontent = field = TEXT
simage = field = BLOB

Schema 
CREATE TABLE PROFILE(id INTEGER PRIMARY KEY, sname TEXT, scontent TEXT, simage BLOB)

これは私がこれまで行ってきたことです。テーブルから任意の名前を選択すると、「ListView.xib」のテキストビューに表示されるように、didselectrowAtIndexpath に何をコーディングする必要がありますか? DB「ListView.xib」のtableViewから選択した名前に従って画像を表示する必要があるため、下部のセクションでUIimageとtextviewフィールドでその名前の画像とコンテンツを確認できます..何か考えはありますか??

4

1 に答える 1

0

名前を ListView に渡したい場合は、以下のコードを使用してください。

人物に関するすべてのデータを保持するためのオブジェクト クラスを作成します。

@interface Person

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *content;
@property (nonatomic, assign) int profileId;

@end

@implementation Person

@synthesize name,content;
@synthesize profileId;

@end

次のようにデータ取得方法を変更します。

Person *person = nil;
NSString *updateSQL = @"SELECT * FROM PROFILE";

const char *update_stmt = [updateSQL UTF8String];

sqlite3_prepare_v2(Dummy2, update_stmt, -1, &statement, NULL);
while(sqlite3_step(statement) == SQLITE_ROW) {
      person = [[Person alloc] init];
      [person setProfileId:(int)sqlite3_column_int(statement, 0)];
      [person setName:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
      [person setContent:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
      [hello addObject:person];
}

tableView クラスで

cellForRowAtIndexPath次のように変更します。

Person *cellvalue = [arr objectAtIndex:indexPath.row];
cell.textLabel.text=cellvalue.name;

などdidSelectRowAtIndexPath

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
    Person *selected = [arr objectAtIndex:indexPath.row];
    txtView.txt =  selected.content
 }

1 提案。

データベースに画像を保存しないでください。データベースが重くなりすぎ、データベースの操作が遅くなります。

1 つの代替方法は、画像をドキュメント ディレクトリに保存し、パスをデータベースに保存することです。

于 2012-11-29T08:24:17.600 に答える