2013-07-19 39 views
0

我在數據庫中插入兩條記錄兩個數據都不同,但是當我得到一個數據並在tableView中顯示。我看到我的第一個數據被tableview中的第二個數據覆蓋,第二個數據在tableview中顯示兩次。無法在ios中的tableview sqlite3中顯示正確的數據

ClsMainPageAppDelegate.h

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

@interface ClsMainPageAppDelegate : UIResponder <UIApplicationDelegate> 
@property (strong, nonatomic) UIWindow *window; 
@property(strong, nonatomic) DBPersonDetails *objDbPerson; 
@end 

ClsMainPageAppDelegate.m

#import "ClsMainPageAppDelegate.h" 
#import "ClsMainPageViewController.h" 
#import "ClsTermsandConditionViewController.h" 

@implementation ClsMainPageAppDelegate 
@synthesize objDbPerson; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
// Override point for customization after application launch. 
return YES; 
} 

DBPersonDetails.h

#import <Foundation/Foundation.h> 

@interface DBPersonDetails : NSObject 

@property (nonatomic, strong) NSString *dbpersonid; 
@property (nonatomic, strong) NSString *dbfullName; 
@property (nonatomic, strong) NSString *dbphoneNumber; 

@end 

DBPersonDetails.m

#import "DBPersonDetails.h" 

@implementation DBPersonDetails 

- (id)init 
{ 
    self = [super init]; 
    if (self) { 

} 
return self; 
} 
@end 

ClsChangeNetworkViewController.h

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

@interface ClsChangeNetworkViewController : UIViewController 
{ 
    NSString *databasePath; 
    sqlite3 *contactDB; 
} 

@property (nonatomic,strong) NSString *name; 
@property (nonatomic,strong) NSString *phone; 

@property (weak, nonatomic) IBOutlet UILabel *HelplineLabel; 

@end 

ClsChangeNetworkViewController.m

#import "ClsChangeNetworkViewController.h" 
#import "DBPersonDetails.h" 
#import "ClsMainPageAppDelegate.h" 

@interface ClsChangeNetworkViewController() <UITableViewDataSource, UITableViewDelegate> 

@property (nonatomic, strong) NSMutableArray *persontableData; 

@end 

@implementation ClsChangeNetworkViewController 
@synthesize HelplineLabel,persontableData; 

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

    // Get the data in database code 

self.persontableData = [[NSMutableArray alloc]init]; 

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: @"contacts.db"]]; 

NSString *dbPath = databasePath; 


if (sqlite3_open([dbPath UTF8String], &contactDB) == SQLITE_OK) 
{ 


    NSString *strSQL; 
    strSQL = @"SELECT * FROM CONTACTS"; 
    const char *sql = (const char *) [strSQL UTF8String]; 
    sqlite3_stmt *stmt; 

    if (sqlite3_prepare_v2(contactDB, sql, -1, &stmt, NULL) == SQLITE_OK) 
    { 


      DBPersonDetails *DBPersonDetail = [[DBPersonDetails alloc]init]; 
      while (sqlite3_step(stmt) ==SQLITE_ROW) 
      { 

       NSString *dbid = [NSString stringWithUTF8String:(char *) sqlite3_column_text(stmt, 0)]; 
       NSLog(@"ID : %@",dbid); 

       NSString *dbname = [NSString stringWithUTF8String:(char *) sqlite3_column_text(stmt, 1) ]; 
       NSLog(@"Name : %@",dbname); 

       NSString *dbphone = [NSString stringWithUTF8String:(char *) sqlite3_column_text(stmt, 2) ]; 
       NSLog(@"Phone Number : %@",dbphone); 


       DBPersonDetail.dbpersonid = dbid; 
       DBPersonDetail.dbfullName = dbname; 
       DBPersonDetail.dbphoneNumber = dbphone; 

       NSLog(@"Data full Name %@ ",DBPersonDetail.dbfullName); 
       NSLog(@"Data Phone Number %@",DBPersonDetail.dbphoneNumber); 

       [self.persontableData addObject:DBPersonDetail]; 
     } 

    } 
    sqlite3_finalize(stmt); 
} 
sqlite3_close(contactDB); 

} 

#pragma mark TableView Delegate 

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

{ 
    return [self.persontableData count]; 

} 



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

static NSString *cellIdentifier = @"Identifier"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
} 
DBPersonDetails *DBPersonDetail = [self.persontableData objectAtIndex:indexPath.row]; 
cell.textLabel.text = DBPersonDetail.dbfullName; 
cell.detailTextLabel.text = DBPersonDetail.dbphoneNumber; 

return cell; 

} 

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

DBPersonDetails *DBPersonDetail = [self.persontableData objectAtIndex:indexPath.row]; 
ClsMainPageAppDelegate *objDbDelegate = [[UIApplication sharedApplication]delegate]; 
objDbDelegate.objDbPerson = DBPersonDetail; 


UIStoryboard *storybrd = self.storyboard; 
ClsChangeNetworkViewController *svc = [storybrd instantiateViewControllerWithIdentifier:@"editcontactcontrol"]; 
[self presentViewController:svc animated:YES completion:nil ]; 

} 

我插入兩個記錄由兩個不同的名字,但我看到第一名稱數據不是在表格視圖中可見並且第二名稱數據在表格視圖中可見兩次。也許問題發生在類ClsChangeNetworkViewController.m in viewDidLoad。

請解決我的問題。

感謝

enter image description here

回答

0

你只創建一個單一的DBPersonDetails對象,然後覆蓋其字段和一個對象添加到您的陣列多次。

爲每個DB步驟創建新對象。

相關問題