单个Sqlite3数据库的两个PickerView

时间:2016-11-09 03:13:05

标签: objective-c sqlite uipickerview

我必须在这里做错事。我的SQLite数据库正在运行。但是,我似乎无法将tname填充到选择器视图中,而选中时会填充三个文本字段tnamelatitudelongitude

信息:两个彼此独立的选择器视图,它们都读取相同的sqlite数据库。

#import "ViewController.h"

@interface ViewController ()
{
    NSMutableArray *array1;
    sqlite3 *towerDB;
    NSString *dbPathString;
}

@end

@implementation ViewController

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

    //UIPickerView *pickerview1;
    //array1 = [[NSMutableArray alloc]init];
    [[self pickerview1]setDelegate:self];
    [[self pickerview1]setDataSource:self];

    [self displayTower];
}

 - (void)displayTower
 {
    array1 = [[NSMutableArray alloc] init];
    sqlite3_stmt *statement;

    if (sqlite3_open([dbPathString UTF8String], &towerDB)==SQLITE_OK) {
        [array1 removeAllObjects];

        NSString *querySql = [NSString stringWithFormat:@"SELECT * FROM TOWERS"];
        const char* query_sql = [querySql UTF8String];

        //  sqlite3_clear_bindings(statement);
        //   sqlite3_reset(statement);

        if (sqlite3_prepare(towerDB, query_sql, -1, &statement, NULL)==SQLITE_OK) // NOT OK
            //This Code works in a TableView!!
            //Will NOT work in a PickerView
        {
            while (sqlite3_step(statement)==SQLITE_ROW)
            {
                NSString *tname = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];
                NSString *latitude = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)];
                NSString *longitude = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 3)];
                // NSString *ds2 = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 4)];
                NSString *PickerTower = [[NSString alloc] initWithFormat:@"%@", tname];
                NSString *lat1 = [[NSString alloc] initWithFormat:@"%@", latitude];
                NSString *long1 = [[NSString alloc] initWithFormat:@"%@", longitude];

                NSArray *array = [[NSArray alloc] initWithObjects:PickerTower, nil];
                [array1 addObject:array];
                NSLog(@"Lat/Long %@ / %@",  lat1, long1);
            }
        }
    }
}

#pragma mark Picker Data Source Methods

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView            {
    //ONE Colume
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    //data view how many items?
    return [array1 count];
    // return [_array2 count];
}

#pragma mark Picker Delegate Methods

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [array1 objectAtIndex:row];

    //return [self->array1 objectAtIndex:row];

    // array1 = [[NSMutableArray alloc] init];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    long select = row;
    NSLog(@"PickerView %ld",  select);
}

@end

1 个答案:

答案 0 :(得分:0)

您必须在方法名称[array1 addObject:array]; NSLog(@"Lat/Long %@ / %@", lat1, long1); 和代码行 -

中更改此代码
[array1 addObject:array];
 NSLog(@"Lat/Long %@ / %@",  lat1, long1);
[[self pickerview1] reloadAllComponents];

with -

 cellTemplate: '<button class="btn primary" ng-click="grid.appScope.deleteRow(row)">Delete</button>'+
    '<button class="btn primary" ng-click="grid.appScope.addRow(row)">Add</button>'

可能会对你有所帮助,或者感觉自由。