if-statement ios检查json参数

时间:2014-03-22 17:20:51

标签: ios objective-c json parsing

我目前正在iOS应用中解析JSON-feed到我的表视图。在JSON-feed中,有两种不同类型的帖子:"type":"post""type":"shared"

当我点击表格视图中的单元格时,会打开一个包含完整帖子等的新视图。我需要在didSelectRowAtIndexPath中设置一个if语句,说:"如果 type =发布,打开此视图。否则,如果 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"
   },

1 个答案:

答案 0 :(得分:1)

在HomeFeedView.h中,只需添加一个新属性,即类型。

@property(strong, nonatomic) NSString *type;

然后在cellForRowAtIndexPath委托方法中设置此类型属性。

- (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"];
    cell.type = [movie objectForKey:@"type"];

    return cell;
}

和didSelect方法写下这个:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //getting the data I'm passing to the next view, where movies = NSMutableArray 
    HomeFeedView *cell = [tableview cellForRowAtIndexPath:indexPath];
    if([cell.type isEqualToString: @"post"]){

    // Allocating the Second View I want to open
    HomePostsObject *newView = [[HomePostsObject alloc] init];

    // passing the data to newView
    newView.theMovie = [self.movies objectAtIndex:indexPath.row];

    // presenting the view
    [self.navigationController pushViewController:newView animated:YES];
   }
   else{

    // Allocating the Second View I want to open
    HomePostsObject *newView = [[HomePostsObject alloc] init];

    // passing the data to newView
    newView.theMovie = [self.movies objectAtIndex:indexPath.row];

    // presenting the view
    [self.navigationController pushViewController:newView animated:YES];
   }
}