【发布时间】: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
在这里您可以看到我的故事板的组合方式以及我使用的参考
【问题讨论】:
标签: objective-c uitableview nsdictionary tableview nsjsonserialization