質問

I am currently having a problem retrieving data from my database. The textlabel.text and the detailTextLabel.text are set.

But all I get is a blank screen on my simulator, with an NSLOG "Queries Loaded" on Xcode.

Does anyone know the problem?

Please help.

#import "ViewController.h"

@interface ViewController ()

{
NSMutableArray *arrayOfCompany;
sqlite3 *companyDB;
NSString *dbPathString;
}


@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
arrayOfCompany = [[NSMutableArray alloc]init];
[[self myTableView]setDelegate:self];
[[self myTableView]setDataSource:self];
[self createOrOpenDB];
}

- (void)createOrOpenDB
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];

dbPathString = [docPath stringByAppendingPathComponent:@"localdb.db"];


NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:dbPathString]) {
    const char *dbPath = [dbPathString UTF8String];

    //creat db here
    if (sqlite3_open(dbPath, &companyDB)==SQLITE_OK) {
        const char *sql_stmt = "CREATE TABLE IF NOT EXISTS storelist (StoreID INTEGER     PRIMARY KEY AUTOINCREMENT, StoreHost TEXT, StorePort TEXT, StoreUser TEXT, StorePass     TEXT)";
        sqlite3_exec(companyDB, sql_stmt, NULL, NULL, NULL);
        sqlite3_close(companyDB);
    }
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayOfCompany count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle     reuseIdentifier:CellIdentifier];
}

Company *aCompany = [arrayOfCompany objectAtIndex:indexPath.row];

cell.textLabel.text = aCompany.storeName;
cell.detailTextLabel.text = aCompany.storeAddress;

return cell;
}




- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)addPersonButton:(id)sender {

char *error;
if (sqlite3_open([dbPathString UTF8String], &companyDB)==SQLITE_OK) {
    NSString *inserStmt = [NSString stringWithFormat:@"INSERT INTO     storelist(StoreHost,StorePort,StoreUser,StorePass) values ('%s', '%s', '%s', '%s')",    [self.storeHost.text UTF8String], [self.storePort.text UTF8String], [self.storeUser.text     UTF8String], [self.storePass.text UTF8String]];

    const char *insert_stmt = [inserStmt UTF8String];

    if (sqlite3_exec(companyDB, insert_stmt, NULL, NULL, &error)==SQLITE_OK) {
        NSLog(@"Company Added");

        Company *company = [[Company alloc]init];


        [company setHost:self.storeHost.text];
        [company setPort:self.storePort.text];
        [company setUser:self.storeUser.text];
        [company setPass:self.storePass.text];



        [arrayOfCompany addObject:company];
    }
    sqlite3_close(companyDB);
    }

}

- (IBAction)displayPersonButton:(id)sender {

sqlite3_stmt *statement;

if (sqlite3_open([dbPathString UTF8String], &companyDB)==SQLITE_OK) {
    [arrayOfCompany removeAllObjects];

    NSString *querySql = [NSString stringWithFormat:@"SHOW DATABASE"];
    const char* query_sql = [querySql UTF8String];

    if (sqlite3_prepare(companyDB, query_sql, -1, &statement, NULL)==SQLITE_OK) {
        while (sqlite3_step(statement)==SQLITE_ROW) {
            NSString *host = [[NSString alloc]initWithUTF8String:(const char     *)sqlite3_column_text(statement, 1)];
            NSString *port = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)];
            NSString *user = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];
            NSString *pass = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];


            Company *company = [[Company alloc]init];


            [company setHost:host];
            [company setPort:port];
            [company setUser:user];
            [company setPass:pass];


            [arrayOfCompany addObject:company];
        }
    }
    }
    [[self myTableView]reloadData];
}

- (IBAction)deletePersonButton:(id)sender {

[[self myTableView]setEditing:!self.myTableView.editing animated:YES];
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
    Company *c = [arrayOfCompany objectAtIndex:indexPath.row];
    [self deleteData:[NSString stringWithFormat:@"Delete from storelist where name is     '%s'", [c.storeName UTF8String]]];
    [arrayOfCompany removeObjectAtIndex:indexPath.row];

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]     withRowAnimation:UITableViewRowAnimationFade];
    }
}

-(void)deleteData:(NSString *)deleteQuery
{
char *error;

if (sqlite3_exec(companyDB, [deleteQuery UTF8String], NULL, NULL, &error)==SQLITE_OK) {
    NSLog(@"Company deleted");
}
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[[self storeHost]resignFirstResponder];
[[self storePort]resignFirstResponder];
[[self storeUser]resignFirstResponder];
[[self storePort]resignFirstResponder];
}
@end
役に立ちましたか?

解決

Since the method numberOfRowsInSection is called at viewDidLoad at which time [arrayOfCompany count] is equal to zero. Therefore, it does not create any cell in the tableView. You have to populate the array before the table view methods get called i.e. either in the viewDidLoad method or initWithNibName method. This issue is resolved with reloadData that you are using, but there are certain issues associated with reloadData.

But if you still intend to keep the buttons and tableview in a single viewcontroller, then this answer might help you:

http://stackoverflow.com/questions/21953009/uitableview-reloaddata-not-working-when-put-it-in-editing-did-end-ibaction-btte
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top