3

私は自分のアプリの1つで、ローカルホストからのデータを解析して、テーブルビューに出力します。データを取得するには、ユーザーは最初にアラートビューを使用してログインします。入力されたユーザーIDは、JSONを使用して解析するデータをフェッチするために使用されます。

この質問には確かに非常に簡単な解決策がありますが、私はそれを修正できないようです。問題は、データを印刷すると、文字列が次の形式になることです。

( "ストリング" )

しかし、私はそれがちょうど言うようにそれが欲しいです:文字列

テーブルビューで。これが私の解析方法です:

 - (void)updateMyBooks
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // Fetch data on a background thread:

    NSString *authFormatString =
    @"http://localhost:8888/Jineel_lib/bookBorrowed.php?uid=%@";

    NSString *string = [[NSString alloc]initWithFormat:@"%@",UserID];

    NSString *urlString = [NSString stringWithFormat:authFormatString, string];

    NSURL *url = [NSURL URLWithString:urlString];

    NSLog(@"uel is %@", url);

    NSString *contents = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

    response1 = [contents JSONValue];

    if (contents) {

        // ... Parse JSON response and add objects to newBooksBorrowed ...
        BookName = [[NSString alloc]init];
        DateBorrowed = [[NSString alloc]init];
        BookID = [[NSString alloc]init];
        BookExtended = [[NSString alloc]init];
        BookReturned = [[NSString alloc]init];

        BookName = [response1 valueForKey:@"BookName"];
        BookID = [response1 valueForKey:@"BookID"];
        DateBorrowed = [response1 valueForKey:@"DateBorrowed"];
        BookExtended = [response1 valueForKey:@"Extended"];
        BookReturned = [response1 valueForKey:@"Returned"];

        dispatch_sync(dispatch_get_main_queue(), ^{
            // Update data source array and reload table view.
            [BooksBorrowed addObject:BookName];
            NSLog(@"bookBorrowed array = %@",BooksBorrowed);
            [self.tableView reloadData];
        });
    }
});
}

これは私がテーブルビューでそれを印刷する方法です:

NSString *string = [[NSString alloc] initWithFormat:@"%@",[BooksBorrowed objectAtIndex:indexPath.row]];

NSLog(@"string is %@",string);
cell.textLabel.text = string;

解析プロセス中にログを使用すると、( "string")として出力されるため、問題は解析のどこかにあります。少なくともそれは私が思うことです。

4

2 に答える 2

4

もしも

NSString *string = [[NSString alloc] initWithFormat:@"%@",[BooksBorrowed objectAtIndex:indexPath.row]];

「(文字列)」のようなものを返す場合、最も可能性の高い理由はそれです

[BooksBorrowed objectAtIndex:indexPath.row]

文字列ではなく、文字列を含む配列です。その場合、

NSString *string = [[BooksBorrowed objectAtIndex:indexPath.row] objectAtIndex:0];

解決策になるはずです。

于 2013-03-22T10:33:50.183 に答える
2
    NSString *string = [[NSString alloc] initWithFormat:@"%@",[BooksBorrowed objectAtIndex:indexPath.row]];
    string = [string stringByReplacingOccurrencesOfString:@"(" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@")" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@"\"" withString:@""];


    NSLog(@"string is %@",string);
    cell.textLabel.text = string;

編集:

上記のコードは、ラベルにその形式のテキストが表示されている場合に使用されます。

あなたがそれを見るなら、NSlogそれはNSString中にありNSArrayます。最初にその文字列を配列からフェッチしてから表示する必要があります。そのためには、@MartinRによって提案されたコード行を使用してください。

NSString *string = [[BooksBorrowed objectAtIndex:indexPath.row] objectAtIndex:0];
于 2013-03-22T10:22:43.343 に答える