我在数据库中插入两条记录,两条数据都不同,但是当我得到一个数据并在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 中出现问题。
请解决我的问题。
由于
答案 0 :(得分:0)
您只创建一个DBPersonDetails
对象,然后覆盖其字段并多次将一个对象添加到您的数组中。
为每个数据库步骤创建新对象。
答案 1 :(得分:0)
最后我的问题解决了。这是一个很棒的博主。
http://iphone4workshop.blogspot.in/2013/04/48-sqlite-create-table-and-retrieve.html
由于