2013-07-24 57 views
-1

我有一個選擇器,其值從數據庫填充(使用NSMutableArray),問題是我試圖添加一個NSString值到我的選擇器(或NSMutableArray的索引0)但沒有什麼是顯示在該位置(位置0)只是一個空白空間和它下面從數據庫中其他值都這個樣子(假設其我的選擇器):NSMutableArray不顯示第一個值

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

------------------------ 
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; 
} 

任何幫助將是一個ppreciated :)

編輯:

這是我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 

再次編輯:

的的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) 
+1

我想因爲你沒有設置「全部」產品的名稱。它將有助於顯示您實際使用此陣列的位置。 – borrrden

+0

你可以發佈你的數組日誌嗎 –

+0

你可以分享選擇器的數據源代碼嗎? – satheeshwaran

回答

1

更改all.allall.name,t應該工作。

作爲評論,您的代碼是不是很可讀。變量的命名很混亂,索引的使用很可怕。如果您可以爲您閱讀的每條記錄執行addObject:,則不需要新增加收藏。

+0

是的,謝謝。它不應該是這樣的,但我想要的是將字符串「全部」添加到我存儲我的數據庫值的數組的第一個索引中,並且讀取了我無法插入已填充的第一個位置陣列? – ukama

+0

是否有無論如何我可以添加字符串「全部」在位置0的數組? – ukama

+1

insertObject:AtIndex:0。 ... geez閱讀sdk人... https://developer.apple.com/library/mac/ipad/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html –

1

選取器無法顯示隨機對象,而是僅顯示一個字符串(在其基本配置中)。確保將Products類的name或其他字符串屬性添加到數組中(或指示您的選擇器的數據源使用正確的字段)。

你應該真的改變你的類和變量的名稱。如果您的班級的一個實例代表產品,則班級名稱應該是Product而不是Products。此外,要使用像all這樣的屬性名稱實際上並不直觀 - 請嘗試考慮更具通用性和描述性的內容。

此外,在您的for循環中,您將i設置爲0,請使用它一次,然後在循環結束時增加它。爲什麼?您的索引集代碼也可以被刪除。

+0

感謝您的建議我有點新這個,所以謝謝 – ukama

+0

這不應該是這樣的,但我想要的是添加字符串「全部」到數組的第一個索引我將我的數據庫值存儲在。我讀了,我不能插入已填充數組的第一個位置? – ukama

+0

當然可以。你可以使用'[array insertObject:id atIndex:0]' – Mundi