无法通过nsmutablearray在选择器视图中添加数据

时间:2011-03-31 11:11:49

标签: iphone nsmutablearray uipickerview

我正在从nsmutableaaray

中的数据库加载数据
-(void)SeoPickerData
{

    self.pickerArray = [[NSMutableArray alloc] init];
    {
        const char *sql = "select * from Main";
        sqlite3_stmt *selectStatement;

        int returnValue = sqlite3_prepare_v2(database1, sql, -1, &selectStatement, NULL);
        if(returnValue == SQLITE_OK)

        {
            //loop all the rows returned by the query.
            while(sqlite3_step(selectStatement) == SQLITE_ROW)
            {
                //int   key = sqlite3_column_int(selectStatement, 0);
                NSString    *Term   = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 2)];

                Container *myObject=[[Container alloc] InitWithTermId:Term];
                [self.pickerArray addObject:myObject];
                [myObject release];
            }
        }
        else
        {
            printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database1) );
        }

    }

}

我将数据复制到另一个数组中,如此

localArray = [[NSMutableArray alloc] init];

Container *classObj = [[Container alloc] init];

[classObj.seoArray removeAllObjects];

[classObj SeoPickerData];

//[localArray addObject:@"Red"];

self.localArray = classObj.pickerArray;

[self.view addSubview:pickerView];

当像uipickerview函数一样加载数组时

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

{

    return [localArray objectAtIndex:row];

}

错误累积,如何在uipicker视图中添加项目?

1 个答案:

答案 0 :(得分:0)

这是一个可能的错误:

NSString    *Term   = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 2)];

Container *myObject=[[Container alloc] InitWithTermId:Term];
[self.pickerArray addObject:myObject];
[myObject release];

您正在向阵列添加Container个对象。

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [localArray objectAtIndex:row];
}

如果您希望返回Container,则会返回NSString个对象。