0

タイムラインを UITableView に表示したいので、次のコードを使用してツイートを取得しています。

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self gettweets];


}

-(void)gettweets {


    NSString *apiurl = [NSString stringWithFormat:@"http://api.twitter.com/1/statuses/user_timeline.json?screen_name=idoodler"];

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:apiurl]];


    NSError* error;
    NSDictionary * json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    NSArray *meta = [json valueForKeyPath:@"text"];
    tweets = json;

    NSLog(@"%@",meta);

}

私のログには正しいツイートが表示されています。

次に、これを使用してつぶやきを UITableView に表示します。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
       //return 0;

    return [tweets count];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CellIdenfifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdenfifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdenfifier];
    }

    cell.text = [tweets objectAtIndex:indexPath.row];
    [tableView reloadData];


    return cell;

}

UITableView の .h ファイルに IBOutlet を作成しました。自分の間違いがどのようなものかわかりません。

4

1 に答える 1

3

次のようなことをする方が良いでしょう:

NSString *apiurl = [NSString stringWithFormat:@"http://api.twitter.com/1/statuses/user_timeline.json?screen_name=idoodler"];
NSError* error = nil;
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:apiurl] options: NSDataReadingUncached error:&error];
if (error)
{
   // Something went wrong
}
else {
  // Data fetched!
  NSDictionary * json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  NSArray *meta = [json valueForKeyPath:@"text"];
  // Setup your tweets array here!
  [tableview reloadData];
}

[tableview reloadData] も削除します。cellForRowAtIndexPath メソッドから。

編集: また、ViewController を、次のようにインターフェイス ビルダーまたは viewDidLoad メソッドでテーブルビューの datatsource/delegate として設定することを忘れないでください。

[tableView setDelegate:self];
[tableView setDataSource:self];
于 2013-04-29T15:31:44.973 に答える