我有一个选择器,其值从数据库填充(使用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
Products.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;
}
LOG:
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)
答案 0 :(得分:1)
选择器不能显示随机对象,而只能显示一个字符串(在其基本配置中)。确保将Products类的name
或其他字符串属性添加到数组中(或指示选择器的数据源使用正确的字段)。
您应该真正更改类和变量的某些名称。如果您的班级的一个实例代表产品,则班级名称应为Product
而不是Products
。此外,使用像all
这样的属性名称实际上并不直观 - 尝试考虑更通用和描述性的内容。
此外,在for循环中,您将i
设置为0
,使用一次,然后在循环结束时增加它。为什么?您的索引集代码也可以消除。
答案 1 :(得分:1)
将all.all
更改为all.name
,并且应该有效。
作为评论,您的代码不易阅读。变量的命名令人困惑,索引的使用是可怕的。如果您可以为您阅读的每条记录addObject:
进行新增加收藏,则无需新增加收藏。