Objective c 简单的twitter程序不起作用
我使用本教程练习创建一个极其基本的twitter应用程序: 我的应用程序中唯一的区别是我只使用tableView ViewController。我好像没法让它工作 ViewController.hObjective c 简单的twitter程序不起作用,objective-c,ios,xcode,twitter,Objective C,Ios,Xcode,Twitter,我使用本教程练习创建一个极其基本的twitter应用程序: 我的应用程序中唯一的区别是我只使用tableView ViewController。我好像没法让它工作 ViewController.h @interface ViewController : UIViewController { NSArray *tweets; } -(void)fetchTweets; @property (retain, nonatomic) IBOutlet UITableView *tableView;
@interface ViewController : UIViewController {
NSArray *tweets;
}
-(void)fetchTweets;
@property (retain, nonatomic) IBOutlet UITableView *tableView;
@end
ViewController.m
#import "ViewController.h"
#import "Twitter/Twitter.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize tableView = _tableView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self fetchTweets];
}
- (void)fetchTweets
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:
[NSURL URLWithString: @"https://api.twitter.com/1/statuses/public_timeline.json"]];
NSError* error;
tweets = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return tweets.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TweetCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *tweet = [tweets objectAtIndex:indexPath.row];
NSString *text = [tweet objectForKey:@"text"];
NSString *name = [[tweet objectForKey:@"user"] objectForKey:@"name"];
cell.textLabel.text = text;
cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", name];
return cell;
}
- (void)viewDidUnload
{
_tableView = nil;
[self setTableView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
@end
您忘了为表定义委托和数据源,并且没有按照我在代码中看到的正确实现协议 请在.h文件中尝试:
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
// your implementation...
}
numberOfRows
,cellForRow
等。。。在定义此表的委托和数据源之前,方法不会起作用:)是否有错误或日志输出?什么不起作用?没有错误,但模拟器中没有检索到推文。我试图NSLog tweet.count来检查数据检索,但它没有输出任何内容。您的异步/分派功能看起来有点难看。请查看NSURLConnectionLegate协议的NSURLConnection。
self.tableView.dataSource = self;
self.tableView.delegate = self;