【问题标题】:Populating Custom TableViewCell from Twitter using JSON Serialization使用 JSON 序列化从 Twitter 填充自定义 TableViewCell
【发布时间】:2015-05-27 15:54:32
【问题描述】:

我是 Objective-C 的新手。我花了无数个小时被困在一个空白的表格视图上,此时我很绝望。 我正在使用他们的 API 通过 JSON 调用加载 Twitter 数据。我将所有内容存储在 NSDictionary 中,运行 for 循环以仅选择“文本”值。我将过滤后的字典存储在一个对象中,稍后在 TableView 初始化中使用该对象。 我为我的自定义单元格创建了 UItableViewCell 的子类。 我的数据源和代表似乎也连接良好(至少我是这么认为的) 我很难找到我的问题。如果有人可以帮助我,请。

#import "ViewController.h"
#import "myCell.h"
#import <TwitterKit/TwitterKit.h>

@interface ViewController ()
@end

@implementation ViewController
@synthesize myTableView;

NSMutableArray *tweetObject;
NSDictionary *dictionary;
NSString *name;
NSString *text;


- (void)viewDidLoad {
    tweetObject = [[NSMutableArray alloc] init];
    [super viewDidLoad];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    text = @"text";
    [[Twitter sharedInstance] logInGuestWithCompletion:^(TWTRGuestSession *guestSession, NSError *error) {
        if (guestSession) {
            // make API calls that do not require user auth
            NSString *statusesShowEndpoint = @"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=goofernator";
            NSError *clientError;
            NSURLRequest *request = [[[Twitter sharedInstance] APIClient]
                                     URLRequestWithMethod:@"GET"
                                     URL:statusesShowEndpoint
                                     parameters:0
                                     error:&clientError];

            if (request) {
                [[[Twitter sharedInstance] APIClient]
                 sendTwitterRequest:request
                 completion:^(NSURLResponse *response,
                              NSData *data,
                              NSError *connectionError) {
                     if (data) {
                         // handle the response data e.g.
                         NSError *jsonError;
                         NSDictionary *json = [NSJSONSerialization
                                               JSONObjectWithData:data
                                               options:0
                                               error:&jsonError];

                         for (NSDictionary *dataDict in json) {
                             NSString *text = [dataDict valueForKeyPath: @"text"];
                             dictionary = [NSDictionary dictionaryWithObjectsAndKeys:text,@"bodytext",nil];
                             [tweetObject addObject:dictionary];
                            }

                     }
                     else {
                         NSLog(@"Error: %@", connectionError);
                     }
                 }];
            }
            else {
                NSLog(@"Error: %@", clientError);
            }
        } else {
            NSLog(@"error: %@", [error localizedDescription]);
        }
    }];
    }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return tweetObject.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    myCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell=[[myCell alloc]initWithStyle:
              UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    NSDictionary *tmpDict = [self.tweetObject objectAtIndex:indexPath.row];

    cell.txtLblOutput.text = [tmpDict objectForKey:text];
    [tableView reloadData];

    return cell;
}

@end

在这里您可以看到我的故事板的组合方式以及我使用的参考

http://postimg.org/image/79w7pqmu3/

http://postimg.org/image/ixq9kabyz/

【问题讨论】:

    标签: objective-c uitableview nsdictionary tableview nsjsonserialization


    【解决方案1】:

    在填充完数组后,您应该在请求完成处理程序中调用 [tableView reloadData];。检查您是否收到任何数据。数组是否被字典对象填充?

    但是伙计,说真的,你需要阅读一些关于编码的好书,你的代码真的缺乏对你在做什么的理解。请从- ...cellForRowAtIndexPath:... 方法中删除[tableView reloadData];

    【讨论】:

    • 感谢您的快速回复,移动[tableView reloadData];请求完成处理程序解决了空 tableview 问题。但是,我尝试加载的数据未显示。它肯定已成功加载(能够加载到 NSLog),但我认为在将其添加到新的 NSdictionary 时我做错了,因为 tableview 返回空值。我不反对,我应该阅读和学习,这正是我想要做的,这是我做/看到 Objective-C 的第二天,我很高兴人们在这里提供帮助。
    • 好的,我明白了...找到了将数组输出到行中的正确方法。
    猜你喜欢
    • 2016-12-26
    • 2014-04-25
    • 1970-01-01
    • 2014-08-02
    • 2014-08-20
    • 1970-01-01
    • 2012-07-31
    • 2011-02-25
    • 2013-10-10
    相关资源
    最近更新 更多