显示要查看的数组值

时间:2013-08-22 04:21:40

标签: iphone objective-c sqlite uiview nsarray

我在服务器中存储了一些值。然后我使用JSON获取该值并添加到本地数据库表。然后我需要显示要查看的值。但是数组值显示在NSLog中。它不会在视图中显示。我不需要在TableView中显示。

代码:

-(void) addDataToArray{


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    //NSLog(@"docs dir is %@", documentsDirectory);

    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"db1.sqlite"];
    //NSLog(@"filepath %@",path);

    mArray = [[NSMutableArray alloc]init];


    if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {

        // const char *sql = "SELECT id,cat_name FROM categories order by order_by";

        const char *sql = "SELECT * FROM categories";

        NSLog(@"Sql is %s",sql);


        sqlite3_stmt *statement;
        int catID = 0;
        if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
            // We "step" through the results - once for each row.
            while (sqlite3_step(statement) == SQLITE_ROW) {


              NSString *catName = [[NSString alloc] initWithUTF8String:
                                     (const char *) sqlite3_column_text(statement, 1)];
                NSLog(@"catName is %@",catName);

                [mArray addObject:catName];

                // [self.view addConstraints:mArray];

                NSLog(@"mArray is %@", mArray);

                [catName release];

                catID = sqlite3_column_int(statement, 0);


            }
        }
        sqlite3_finalize(statement);
    } else {
        sqlite3_close(database);
        NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
        // Additional error handling, as appropriate...
    }
}



- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"connectionDidFinishLoading");
[self addDataToArray];
}

的NSLog:

catName is person1
mArray is (
    person1
)

catName is person2

mArray is (
    person1
    person2
)

的TextView:

    UITextView *txt=[[UITextView alloc]initWithFrame:CGRectMake(50, 50, 200, 200)];
      //  txt.text=[mArray objectAtIndex:0];
        [self.view addSubview:txt];

for (int i = 0; i<[mArray count]; i++ ) { NSLog(@"index %d",i); }

1 个答案:

答案 0 :(得分:1)

您需要合并array's个对象才能在string中对其进行转换,因为您无法直接在array中显示textView。如果mArray包含NSString类型对象,那么您可以这样做:

NSMutableString *string = [[NSMutableString alloc] init];
for (id obj in mArray){
    [string appendString:obj];
}

textView.text = string;

如果您想在下一行显示每个person,可以在每个名称后添加\n