我定義類數據類有我有這個方法NSMutableArray裏沒有保留價值
+(NSMutableArray*) returnList :(NSString *) sql
{
NSMutableArray *tmpList=[[[NSMutableArray alloc]init]autorelease];
sqlite3 *db;
NSString *dbPath =[[NSBundle mainBundle] pathForResource:@"DB_MyReef" ofType:@"sqlite"];
if (sqlite3_open([dbPath UTF8String],&db) ==SQLITE_OK)
{
sqlite3_stmt *ss;
int rv = sqlite3_prepare_v2(db, [sql UTF8String], -1,&ss,NULL);
if (rv==SQLITE_OK)
{
while (sqlite3_step(ss)==SQLITE_ROW)
{
NSString *txt =[NSString stringWithUTF8String:(char *)sqlite3_column_text(ss,0)];
[tmpList addObject:txt];
[txt release];
}
}
sqlite3_finalize(ss);
}
sqlite3_close(db);
return tmpList;
}
在我命名UserSelect我創建其他類。
以h文件
@interface UserSelect : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource> {
NSMutableArray *List;
UIPickerView *myPicker;
}
@property (nonatomic,retain) NSMutableArray *List;
@property (nonatomic,retain) UIPickerView *myPicker;
@end
且m文件
- (void)viewDidLoad {
[super viewDidLoad];
//List=[[NSMutableArray alloc] init;
NSString *sql;
//Loading Data
if ([self.title isEqualToString:@"Marca Teste"])
{
[email protected]"SELECT TESTMARK FROM TBL_TESTSMARK ORDER BY TESTMARK";
}
else
{
[email protected]"SELECT PT_PARAMNAME FROM TBL_PARAMETERS "
"WHERE COD IN "
"(SELECT A.CODTEST FROM TBL_TEST AS A "
"INNER JOIN "
"TBL_TESTSMARK AS B "
"ON "
"A.CODTESTMARK =B.COD "
"WHERE "
"B.TESTMARK='Salifert')";
}
self.List=[[NSMutableArray alloc] init];
self.List=[[NSMutableArray arrayWithArray:[DataClass returnList:sql]]retain];
myPicker=[[UIPickerView alloc] init];
myPicker.delegate=self;
myPicker.dataSource=self;
/*for (NSString *ts in List) {
NSLog(@"%@",ts);
}*/
[self.view addSubview:myPicker];
}
好,我調用此方法,並預期一個該返回。
但在UiPickerView方法中,我的List變量只保留NSMutable數組中的第一個對象。
這些都是UIPicker方法
#pragma mark pickerView
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
NSLog(@"%@",@"numberOfComponentsInPickerView");
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSLog(@"%@",@"numberOfRowsInComponent");
return [List count];
}
-(NSString *)pickerView:(UIPickerView *) pickerView titleForRow:(NSInteger) row forComponent:(NSInteger) component
{
NSString *txt=[[NSString alloc] initWithFormat:@"%@",[self.List objectAtIndex:(int)row]];
return txt;
[txt release];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSString *txt=[[NSString alloc] initWithFormat:@"%@",[self.List objectAtIndex:(int)row]];
[DataClass writePlist:txt toKey:TEST_MARK];
[txt release];
}
我試圖引導列表的方法很多,什麼`錯了嗎?
您從rudy中得到了一個很好的答案,我只想添加下面這行是不必要的:'self.List = [[NSMutableArray alloc] init];'因爲您將該屬性設置爲下一行的其他內容。 –
超過不必要;它會泄漏創建的數組。/cc @fichek –
這段代碼經過很多改變,所以爲什麼他根本不工作。 但仍不明白它是如何工作的回報。 謝謝 –