0
저는 ARC 및 스토리 보드 (iOS 5)를 사용하는 xCode 4.2에 있습니다. 내 TableViewController에 짹짹을 넣으려고합니다. 내 NSURLConnection을 배치하면 모든 것이 잘되고 대리자의 트윗 (NSArray)에 응답이 이미 있습니다. 문제는 테이블 뷰를 채우지 않는다는 것입니다. 받은 데이터에 대해 SBJson을 사용하고 있습니다. 내가 놓친 게 있니?AppDelegate의 NSURLConnection 및 SBJSon
AppDelegate.m
#import "AppDelegate.h"
#import "TwitterViewController.h"
#import "SBJson.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize receivedData;
@synthesize tableViewController;
@synthesize tweets;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
tweets = [NSMutableArray array];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"http://search.twitter.com/search.json?q={username}&rpp=5"]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
if (theConnection) {
receivedData = [NSMutableData data];
}
return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSDictionary *results = [responseString JSONValue];
NSArray *allTweets = [results objectForKey:@"results"];
[tableViewController setTweets:allTweets];
}
@end
이 내 테이블 뷰 컨트롤러에서 위임 방법이다.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tweets count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"twitter";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *aTweet = [tweets objectAtIndex:[indexPath row]];
NSString *textFrom = [aTweet objectForKey:@"text"];
cell.textLabel.text = textFrom;
return cell;
}
정말 조언이 필요합니다.