0

我在我的服务器中存储了一些值。然后我使用 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];
}

NS日志:

catName is person1
mArray is (
    person1
)

catName is person2

mArray is (
    person1
    person2
)

文本视图:

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

1 回答 1

1

您需要合并array's对象以将它们转换为 in string,因为您不能直接显示arrayin textView。如果mArray包含NSString类型对象,那么你可以这样做:

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

textView.text = string;

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

于 2013-08-22T04:50:28.767 回答