Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 无法在ios中的tableview sqlite3中显示正确的数据_Iphone_Ios_Uitableview_Sqlite - Fatal编程技术网

Iphone 无法在ios中的tableview sqlite3中显示正确的数据

Iphone 无法在ios中的tableview sqlite3中显示正确的数据,iphone,ios,uitableview,sqlite,Iphone,Ios,Uitableview,Sqlite,我在数据库中插入了两条记录,这两条数据是不同的,但当我得到一条数据并显示在tableView中时。我看到我的第一个数据在tableview中被第二个数据覆盖,第二个数据在tableview中显示两次 ClsMainPageAppDelegate.h DBPersonDetails.h ClsChangeNetworkViewController.h ClsChangeNetworkViewController.m 我用两个不同的名称插入了一个两个记录,但我看到第一个名称数据在表视图中不可见,第二

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

ClsMainPageAppDelegate.h

DBPersonDetails.h

ClsChangeNetworkViewController.h

ClsChangeNetworkViewController.m

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

请解决我的问题

谢谢

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


为每个数据库步骤创建新对象。

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

谢谢

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

@interface ClsMainPageAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property(strong, nonatomic) DBPersonDetails *objDbPerson;
@end
#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;
}
#import <Foundation/Foundation.h>

@interface DBPersonDetails : NSObject

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

@end
#import "DBPersonDetails.h"

@implementation DBPersonDetails

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

}
return self;
}
@end
#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
#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 ];

}