【发布时间】:2014-03-22 17:20:50
【问题描述】:
我目前正在我的 iOS 应用程序中将 JSON 提要解析到我的表格视图中。在 JSON-feed 中有两种不同类型的帖子:"type":"post" 和 "type":"shared"。
当我单击表格视图中的单元格时,会打开一个包含完整帖子等的新视图。我需要在 didSelectRowAtIndexPath 中设置一个 if 语句,说:“如果 type = post em>,打开这个视图。否则,如果 type = shared,打开这个视图”。所以我需要以某种方式检查 json 参数类型。并在此基础上得出两种不同的观点。
didSelectRowAtIndexPath 方法目前看起来像这样:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//getting the data I'm passing to the next view, where movies = NSMutableArray
NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
// Allocating the Second View I want to open
HomePostsObject *newView = [[HomePostsObject alloc] init];
// passing the data to newView
newView.theMovie = movie;
// presenting the view
[self.navigationController pushViewController:newView animated:YES];
}
解析数据的完整代码如下所示:
@implementation HomeViewController
@synthesize profilePic;
@synthesize tableView = _tableView, activityIndicatorView = _activityIndicatorView;
@synthesize btnFaceBook, btnTwitter, btnTwitter2;
@synthesize strURLToLoad;
@synthesize movies;
@synthesize statsHome;
@synthesize fontForCellText;
@synthesize twitterFollowers;
@synthesize facebookLikes;
@synthesize instagramFollowers;
- (void)viewDidLoad
{
[super viewDidLoad];
// Initializing Data Source
movies = [[NSMutableArray alloc] init];
NSURL *url = [[NSURL alloc] initWithString:@"link.php"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
self.movies = [[NSArray alloc] initWithArray:JSON];
[self.activityIndicatorView stopAnimating];
[self.tableView reloadData];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return movies.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 85;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"HomeFeedView";
HomeFeedView *cell = (HomeFeedView *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HomeFeedView" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.backgroundColor = [UIColor clearColor];
}
NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
cell.title.text = [movie objectForKey:@"message"];
cell.published.text = [movie objectForKey:@"published"];
cell.twitterName.text = [movie objectForKey:@"celebname"];
return cell;
}
- (NSString *)getTextKey
{
return @"message";
}
- (NSString *)getPostedTime
{
return @"published";
}
- (NSString *)getTwitterName
{
return @"celebname";
}
- (CGFloat)getHeightForText:(NSString *)strText
{
CGSize constraintSize = CGSizeMake(cellTextWidth, MAXFLOAT);
CGSize labelSize = [strText sizeWithFont:fontForCellText constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
NSLog(@"labelSize.height = %f",labelSize.height);
return labelSize.height;
}
@end
JSON 结构/数据:
{
"message":"blablablabla",
"published":"5 hours ago",
"link":"http://google.com",
"unix":1395493814,
"name":"Name",
"story":null,
"picture":"some-pic.jpg",
"type":"shared"
},
{
"message":"blablabla",
"published":"5 hours ago",
"name":"Name",
"unix":1395493814,
"type":"post"
},
【问题讨论】:
-
“类型”值在哪里,在 JSON 的正文中,还是在请求状态中? (向我们展示一些实际的 JSON。)
-
检查我的编辑@HotLicks
-
你保留原来的 JSON 数组,为什么不直接在 didSelectRowAtIndexPath 中使用
NSString type = self.movies[indexPath.row][@"type"];来访问该类型? -
是的,这就是我要做的
标签: ios objective-c json parsing