-1

我有一个选择器,它的值是从数据库中填充的(使用 NSMutableArray),问题是我正在尝试将一个 NSString 值添加到我的选择器(或 NSMutableArray)的索引 0 中,但没有任何内容只显示一个空格在该位置(位置 0)及其下方,数据库中的其他值如下所示(假设它是我的选择器):

------------------------

------------------------
Mouse
------------------------
Keyboard
------------------------
Motherboard
------------------------

这是我用来从数据库中检索数据的代码:

-(NSMutableArray *)getProducts
{
    NSMutableArray *products = [[NSMutableArray alloc] init];
    Products *all = [[Products alloc]init];
    NSString allstring= @"All";
    all.all= allstring; // the "all" is a NSString type variable declared in Products class
    [products addObject:all];

    NSMutableArray *newadditions = [[NSMutableArray alloc]init];
    NSMutableIndexSet *indexes =[NSMutableIndexSet indexSetWithIndex:1];
    [indexes addIndex:2];
    [indexes addIndex:3];
    const char* sql = "SELECT ID,Name \
    FROM Products";

    sqlite3_stmt *statement;
    int sqlResult = sqlite3_prepare_v2(database, sql, -1, &statement, NULL);

    if(sqlResult == SQLITE_OK)
    {
        while(sqlite3_step(statement)==SQLITE_ROW)
        {
            int i=0;
            Products *product =[[Products alloc]init];

            char*name = (char*)sqlite3_column_text(statement, 1);

            product.name = (name)?[NSString stringWithUTF8String:name]: @"";

            [newadditions insertObject:product atIndex:i];
            i++;

        }


        [products insertObjects:newadditions atIndexes:indexes];


        sqlite3_finalize(statement);
    }
    else
    {
        NSLog(@"Problem with the database");
        NSLog(@"%d",sqlResult);
    }
    return products;
}

任何帮助,将不胜感激 :)

编辑:

这是我的 Products.h

@interface Products : NSObject
{
    NSString *name;
    NSString *all;
}

@property (strong,nonatomic) NSString *name;
@property (strong,nonatomic) NSString *all;
@end

产品.m:

#import "Products.h"

@implementation Products

@synthesize name;
@synthesize all;

@end

以及我打电话给选择器的地方:

@interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate>

@property (strong, nonatomic) IBOutlet PRLabel *namesLabel;

@property (strong, nonatomic) UIPickerView* namesPicker;

@property (strong, nonatomic) NSMutableArray *namesAll;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.namesPicker = [[UIPickerView alloc] init];
    self.namesPicker.dataSource = self;
    self.namesPicker.delegate = self;
    self.namesPicker.showsSelectionIndicator = YES;
    self.namesLabel.inputView = [self namesPicker];
    self.namesLabel.inputAccessoryView = [self accessoryToolbar];


    DBAccess *dbAccess = [[DBAccess alloc]init];
    self.namesAll = [dbAccess getProducts];

    [dbAccess closeDatabase];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



#pragma mark - UIPickerViewDataSource

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
        return [self.namesAll count];


}

#pragma mark - UIPickerViewDelegate

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

        Products * prod = [self.namesAll objectAtIndex:row];
        return prod.type;


}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

        self.namesLabel.text = [self.namesPicker.delegate pickerView:pickerView titleForRow:row forComponent:component];



}

@end

再次编辑:

在我尝试将“All”字符串添加到数组的第一个位置之前的 getProducts:

-(NSMutableArray *)getProducts
{
    NSMutableArray *products = [[NSMutableArray alloc] init];
    const char* sql = "SELECT ID,Name \
    FROM Products ";

    sqlite3_stmt *statement;
    int sqlResult = sqlite3_prepare_v2(database, sql, -1, &statement, NULL);

    if(sqlResult == SQLITE_OK)
    {
        while(sqlite3_step(statement)==SQLITE_ROW)
        {
            Product *product =[[Product alloc]init];

            char*name = (char*)sqlite3_column_text(statement, 1);

            product.name = (name)?[NSString stringWithUTF8String:name]: @"";

            [products addObject:product];

        }

         NSLog(@"%@",products);
        sqlite3_finalize(statement);
    }
    else
    {
        NSLog(@"Problem with the database");
        NSLog(@"%d",sqlResult);
    }
    return products;
}

日志:

2013-07-24 13:49:56.425 just[1401:c07] Opening Database
2013-07-24 13:49:56.433 just[1401:c07] (
    All,
    "<Product: 0x719f350>",
    "<Product: 0x719fcb0>",
    "<Product: 0x719ff30>"
)

2013-07-24 13:49:58.053 just[1401:c07] -[__NSCFConstantString name]: unrecognized selector sent to instance 0xd938
2013-07-24 13:49:58.054 just[1401:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString name]: unrecognized selector sent to instance 0xd938'
*** First throw call stack:
(0x20a1012 0x11aee7e 0x212c4bd 0x2090bbc 0x209094e 0x6a06 0xeb6fc 0xee886 0x1ad8fb 0x1ad9cf 0x1961bb 0x194872 0x19f5d4 0x52e299 0xed27a 0xed10c 0x1432dd 0x11c26b0 0x269dfc0 0x269233c 0x269deaf 0x4a23fe 0x49b798 0x49ca34 0x49e8a2 0x49e931 0x49e97b 0x498117 0x201386 0x200e29 0x2935 0x125cef 0x125f02 0x103d4a 0xf5698 0x1ffcdf9 0x1ffcad0 0x2016bf5 0x2016962 0x2047bb6 0x2046f44 0x2046e1b 0x1ffb7e3 0x1ffb668 0xf2ffc 0x250d 0x2435)
libc++abi.dylib: terminate called throwing an exception
(lldb) 
4

2 回答 2

1

更改all.allall.name, 并且 t 应该可以工作。

作为评论,您的代码不是很可读。变量的命名令人困惑,索引的使用令人恐惧。addObject:如果您可以为您阅读的每条记录执行此操作,则不需要 newadditions 集合。

于 2013-07-24T10:21:33.303 回答
1

选择器不能显示随机对象,而只能显示一个字符串(在其基本配置中)。确保将nameProducts 类的或其他字符串属性添加到数组中,(或指示选择器的数据源使用正确的字段)。

你真的应该改变你的类和变量的一些名称。Product如果您的类的一个实例代表一种产品,则类名不应为Products。此外,使用类似的属性名称all确实不直观 - 尝试考虑更通用和描述性的东西。

此外,在您设置i为的 for 循环中0,使用一次,然后在循环结束时增加它。为什么?您的索引集代码也可以消除。

于 2013-07-24T10:15:52.593 回答