UITableView(带有sqlite3)没有更新

时间:2013-11-24 17:08:35

标签: ios iphone uitableview sqlite

我是韩国人,我的英语很简单 非常抱歉 请帮帮我

我的应用示例电话簿。 两个带有sqlite3 DB的视图和标签栏

第一个视图是表格视图单元格列表,有名称是链接DB
第二个视图使用SQL查询保存并查找和删除电话簿列表。

我希望第二次查看保存数据并更改标签更新数据列表。

我编码[viewDidAppear] reloadData方法

关闭应用程序重新打开应用程序已更新。

但未更新更改点按T_T请帮帮我。

FirstViewController.h

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

@interface FirstViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
    NSString *path;
    NSString *dbName;
    NSString *databasePath;
    sqlite3 *contactDB;
}
@property (nonatomic) sqlite3 *contactDB_2;
@property (strong,nonatomic) IBOutlet UITableView *myTable;
@property (nonatomic,retain) NSMutableArray *quizs;

-(void)checkAndCreateDatabase;
-(void)readFromDatabase;

@end

FistViewController.m

#import "FirstViewController.h"
#import "person.h"

@implementation FirstViewController

@synthesize contactDB_2,myTable;
@synthesize quizs;

-(void)checkAndCreateDatabase{
NSFileManager *filemgr = [NSFileManager defaultManager];
if([filemgr fileExistsAtPath:databasePath] == NO){
    const char *dbPath = [databasePath UTF8String];
    if(sqlite3_open(dbPath,&contactDB) == SQLITE_OK){
        char *errMsg;
        const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,ADRESS TEXT, PHONE TEXT)";
        sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg);
        sqlite3_close(contactDB);
    }
}
}
-(void)readFromDatabase
{
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if(sqlite3_open(dbpath, &contactDB)==SQLITE_OK)
{
    NSString *querySQL = [NSString stringWithFormat:@"SELECT name,phone FROM contacts"];
    const char *query_stmt = [querySQL UTF8String];
    if(sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL)==SQLITE_OK)
    {
        while(sqlite3_step(statement)==SQLITE_ROW){
            NSString* nameField = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 0)];
            NSString* phoneField = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 1)];

            person *persons = [person alloc];

            [persons setName:nameField];
            [persons setPhone:phoneField];

            [quizs addObject:persons];

        }
        sqlite3_finalize(statement);
        [myTable reloadData];
    }
    sqlite3_close(contactDB);
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    self.title = NSLocalizedString(@"전화번호목록", @"전화번호목록");
    self.tabBarItem.image = [UIImage imageNamed:@"first"];
}
return self;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

pragma mark - View lifecycle

- (void)viewDidLoad
{
dbName = @"contacts2.db";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:dbName];
[self checkAndCreateDatabase];
quizs = [[NSMutableArray alloc] initWithCapacity:100];
[self readFromDatabase];
[super viewDidLoad];
}

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

}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[myTable delegate];

}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[quizs removeAllObjects];
[self checkAndCreateDatabase];
[self readFromDatabase];
[myTable reloadData];
}


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

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return quizs.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* cellIdentifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if(cell == nil)
{
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

}
person *p = [quizs objectAtIndex:indexPath.row];
NSString *str = [[NSString alloc]initWithFormat:@"%@ %@",p.name,p.phone];
cell.textLabel.text = str;

return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = indexPath.row;

NSString *temp = [[NSString alloc]initWithFormat:@"%d번쨰꺼 선택해뜸",row+1];

UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"터치" message:temp delegate:self cancelButtonTitle:nil otherButtonTitles:@"확인", nil];

[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

我想更改标签并更新单元格列表。

1 个答案:

答案 0 :(得分:0)

实际上你无法在后台插入数据库。因此,在插入所有数据之前,你必须保持应用程序存活。如果所有数据都已成功保存,请在ViewWillAppear中编写reloadData代码。