NSMutableArray未显示第一个值



我有一个选择器,它的值是从数据库中填充的(使用NSMutableArray),问题是我试图将NSString值添加到我的选择器(或NSMutaableArray)的索引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;
}

如有任何帮助,我们将不胜感激:)

编辑:

这是我的产品。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

再次编辑:

getProducts,然后我尝试将"All"字符串添加到数组的第一个位置:

-(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) 

all.all更改为all.name,t应该可以工作。

作为注释,您的代码可读性不是很好。变量的命名令人困惑,索引的使用令人恐惧。如果您可以对读取的每个记录执行addObject:,则不需要重新添加集合。

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

您确实应该更改类和变量的一些名称。如果类的一个实例代表一个产品,那么类名应该是Product,而不是Products。此外,使用像all这样的属性名称真的不直观——试着想出一些更通用、更具描述性的名称。

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

相关内容

最新更新