0

我在数据库中插入了两条记录,两个数据都不同,但是当我获取数据并显示在 tableView 中时。我看到我的第一个数据被 tableview 中的第二个数据覆盖,第二个数据在 tableview 中显示两次。

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

}

我插入了两个不同名称的两条记录,但我看到名字数据在表视图中不可见,而第二名数据在表视图中可见两次。viewDidLoad 中的ClsChangeNetworkViewController.m类中可能出现问题。

请解决我的问题。

谢谢

在此处输入图像描述

4

2 回答 2

0

您只需创建一个DBPersonDetails对象,然后覆盖其字段并将一个对象多次添加到您的数组中。

为每个 DB 步骤创建新对象。

于 2013-07-19T18:10:53.580 回答
0

终于我的问题解决了。这是一个很棒的博主。

http://iphone4workshop.blogspot.in/2013/04/48-sqlite-create-table-and-retrieve.html

谢谢

于 2013-07-19T22:02:41.550 回答