【问题标题】:Show twitter timeline in UITableview在 UITableview 中显示 Twitter 时间线
【发布时间】:2013-04-23 04:22:09
【问题描述】:

我喜欢在 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;

}

我在 .h 文件中为 UITableView 制作了一个 IBOutlet。我真的不知道我的错误是什么!

【问题讨论】:

  • 也许这可能有助于您了解 UITableView iosdevnotes.com/2011/10/uitableview-tutorial
  • 我试过了,但它不起作用,请检查我的问题,我添加了我的 TableView 代码
  • 那么你应该调用 [tableView reloadData];在获取数据后立即
  • @art-divin 好的,我添加了行,但它不起作用,你能提供一些代码吗?
  • 我无法理解您所指的代码的哪一部分存在错误。请说明您的代码究竟是如何不工作的,您的类的设置是什么,您在哪种方法中调用提要的下载。

标签: objective-c arrays uitableview twitter


【解决方案1】:

最好是这样做:

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 方法。

编辑: 另外不要忘记在界面构建器或 viewDidLoad 方法中将 ViewController 设置为 tableview 的数据源/委托,如下所示:

[tableView setDelegate:self];
[tableView setDataSource:self];

【讨论】:

  • 好的,我必须把数据源从TableView拖到ViewController吗?
  • 一个问题,如何停止 UIRefreshControl?
猜你喜欢
  • 2014-05-18
  • 1970-01-01
  • 1970-01-01
  • 2014-03-27
  • 1970-01-01
  • 2017-12-25
  • 1970-01-01
  • 2013-09-06
  • 2020-02-01
相关资源
最近更新 更多