Objective c 使用JSON序列化从Twitter填充自定义TableViewCell

Objective c 使用JSON序列化从Twitter填充自定义TableViewCell,objective-c,uitableview,nsdictionary,tableview,nsjsonserialization,Objective C,Uitableview,Nsdictionary,Tableview,Nsjsonserialization,我是Objective-C的新手。我花了无数个小时才得到一个空白的桌面视图,现在我绝望了。 我使用他们的API通过JSON调用加载twitter数据。我将所有内容存储在NSDictionary中,运行for循环以仅选择“text”值。我将过滤后的字典存储在一个对象中,稍后在TableView初始化中使用该对象。 我为自定义单元格创建了UItableViewCell的子类。 我的数据源和代理似乎也连接良好(至少我是这么认为的) 我很难找到我的问题。如果有人能帮我的话 #import "ViewCo

我是Objective-C的新手。我花了无数个小时才得到一个空白的桌面视图,现在我绝望了。 我使用他们的API通过JSON调用加载twitter数据。我将所有内容存储在NSDictionary中,运行for循环以仅选择“text”值。我将过滤后的字典存储在一个对象中,稍后在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
#导入“ViewController.h”
#导入“myCell.h”
#进口
@界面视图控制器()
@结束
@实现视图控制器
@综合myTableView;
NSMutableArray*tweetObject;
NSDictionary*字典;
NSString*名称;
NSString*文本;
-(无效)viewDidLoad{
tweetObject=[[NSMutableArray alloc]init];
[超级视图下载];
self.myTableView.dataSource=self;
self.myTableView.delegate=self;
text=@“text”;
[[Twitter sharedInstance]登录完成:^(TWTRGuestSession*guestSession,NSError*error){
if(访客会话){
//进行不需要用户身份验证的API调用
NSString*状态ShowEndpoint=@”https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=goofernator";
n错误*clientError;
NSURLRequest*request=[[[Twitter sharedInstance]APIClient]
URLRequestWithMethod:@“获取”
URL:StatuseShowEndpoint
参数:0
错误:&clientError];
如果(请求){
[[[Twitter sharedInstance]APIClient]
sendTwitterRequest:请求
完成:^(NSURLResponse*响应,
NSData*数据,
N错误*连接错误){
如果(数据){
//处理响应数据,例如。
n错误*jsonError;
NSDictionary*json=[NSJSONSerialization
JSONObjectWithData:数据
选项:0
错误:&jsonError];
for(json格式的NSDictionary*dataDict){
NSString*text=[dataDict valueForKeyPath:@“text”];
dictionary=[NSDictionary dictionary WithObjectsAndKeys:text,@“bodytext”,nil];
[tweetObject addObject:dictionary];
}
}
否则{
NSLog(@“错误:%@”,connectionError);
}
}];
}
否则{
NSLog(@“错误:%@”,clientError);
}
}否则{
NSLog(@“错误:%@,[error localizedDescription]);
}
}];
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回tweetObject.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
myCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[myCell alloc]initWithStyle:
UITableViewCellStyleSubtitle重用标识符:CellIdentifier];
}
NSDictionary*tmpDict=[self.tweetObject对象索引:indexath.row];
cell.txtLblOutput.text=[tmpDict objectForKey:text];
[tableView重新加载数据];
返回单元;
}
@结束
在这里,你可以看到我的故事板是如何组合在一起的,以及我使用的参考资料


您应该调用
[tableView reloadData]。检查是否收到任何数据。数组中是否填充了字典对象


但是,伙计,说真的,你需要读一些关于编码的好书,你的代码真的不了解你在做什么。请删除
[tableView reloadData]
-…cellForRowAtIndexPath:…
方法。感谢您的快速回复,移动[tableView reloadData];请求完成处理程序解决了空tableview问题。但是,我尝试加载的数据没有显示。它肯定是成功加载的,(能够NSLog),但我认为我在将它添加到新的NSdictionary时出错了,因为tableview返回空值。我不反对,我应该阅读和学习,这正是我想做的,这是我做/看objective-C的第二天,我很高兴大家在这里提供了帮助。好的,我明白了…找到了将数组输出到行中的正确方法。