Objective c 在UITableview中显示twitter时间线
我喜欢在UITableView中显示时间线,我使用以下代码获取推文:Objective c 在UITableview中显示twitter时间线,objective-c,arrays,uitableview,twitter,Objective C,Arrays,Uitableview,Twitter,我喜欢在UITableView中显示时间线,我使用以下代码获取推文: - (void)viewDidLoad { [super viewDidLoad]; [self gettweets]; } -(void)gettweets { NSString *apiurl = [NSString stringWithFormat:@"http://api.twitter.com/1/statuses/user_timeline.json?screen_name=id
- (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;
}
我在.h文件中为UITableView创建了一个IBOutlet。我真的不知道我的错误是什么 最好是这样做:
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 setDelegate:self];
[tableView setDataSource:self];
同时删除[tableview重载数据];来自CellForRowatineXpath方法
编辑:
另外,不要忘记在interface builder或viewDidLoad方法中将ViewController设置为tableview的datatsource/delegate,如下所示:
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 setDelegate:self];
[tableView setDataSource:self];
最好是这样做:
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 setDelegate:self];
[tableView setDataSource:self];
同时删除[tableview重载数据];来自CellForRowatineXpath方法
编辑:
另外,不要忘记在interface builder或viewDidLoad方法中将ViewController设置为tableview的datatsource/delegate,如下所示:
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 setDelegate:self];
[tableView setDataSource:self];
也许这可能有助于您了解UITableView我尝试过它,但它不起作用,请检查我的问题,我添加了我的TableView代码,然后您应该调用[TableView reloadData];就在数据公布之后fetched@art-divin好的,我添加了行,但行不通,你能给我一些代码吗?我不明白你所指的代码的哪一部分有错误。请具体说明你的代码到底是如何工作的,你的类的设置是什么,你用什么方法调用提要下载。也许这有助于你了解UITableView我尝试过它,但它不起作用,请检查我的问题,我添加了我的TableView代码,然后你应该调用[TableView reloadData];就在数据公布之后fetched@art-divin好的,我添加了行,但行不通,你能给我一些代码吗?我不明白你所指的代码的哪一部分有错误。请指定代码的工作方式、类的设置以及调用提要下载的方法。好的,我必须将数据源从TableView拖到ViewController吗?一个问题,我如何停止UIRefreshControl?好的,我必须将数据源从TableView拖到ViewController吗?一个问题,如何停止该控件?