1

Im new to xcode. and Im creating this facebook, twitter, instagram and youtube news feed in a tableview

at first Im trying this facebook to load multiple json

#import "ViewController.h"
#import "DetailViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>


#define kQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define kJSONUrl [NSURL URLWithString:@"https://www.facebook.com/feeds/page.php?id=1387524391527078&format=json"]
#define kJSONUrl1 [NSURL URLWithString:@"https://www.facebook.com/feeds/page.php?id=196934133652011&format=json"]
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)



@interface ViewController ()

@end

@implementation ViewController
@synthesize jsonTitles,table,jsonContent;





-(void)loadJSON:(NSData *)responseData {

NSError *error;
NSArray *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

NSArray *array = [json valueForKey:@"entries"];

for (int i = 0; i < array.count; i++) {

    NSArray *entry = [array objectAtIndex:i];

    NSString *title = [entry valueForKey:@"title"];
    NSString *content = [entry valueForKey:@"content"];


    [jsonTitles addObject:title];
    [jsonContent addObject:content];


}


[self.table reloadData];


}


- (void)viewDidLoad
{
self.table.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
jsonTitles = [[NSMutableArray alloc]init];
jsonContent = [[NSMutableArray alloc]init];


dispatch_async(kQueue, ^{
    NSData *data = [NSData dataWithContentsOfURL:kJSONUrl];
    [self performSelectorOnMainThread:@selector(loadJSON:) withObject:data waitUntilDone:YES];


    NSData *data1 = [NSData dataWithContentsOfURL:kJSONUrl1];
    [self performSelectorOnMainThread:@selector(loadJSON:) withObject:data1 waitUntilDone:YES];


});



[super viewDidLoad];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

return 40;


}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


return [jsonTitles count];

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

[self performSegueWithIdentifier:@"detailSegue" sender:nil];
// NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"detailSegue" ascending:YES];



}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([segue.identifier isEqualToString:@"detailSegue"]) {

    DetailViewController *detail = segue.destinationViewController;

    NSIndexPath *path = [self.table indexPathForSelectedRow];

    detail.titleText = [jsonTitles objectAtIndex:path.row];
    detail.contentText = [jsonContent objectAtIndex:path.row];




}


}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {





static NSString *tableCellID = @"tableCellID";

// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableCellID];

UITableViewCell *cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                        reuseIdentifier:tableCellID];




NSString *myurl = @"http://graph.facebook.com/idoltap/picture";
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:myurl]];
UIImage *myimage = [[UIImage alloc] initWithData:imageData];

cell.imageView.image = myimage;


// cell.imageView.image = [UIImage imageNamed:@"background.png"];
cell.textLabel.text = @"iDOLTap";

if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableCellID];


}

cell.detailTextLabel.text = [jsonTitles objectAtIndex:indexPath.row];
if ([cell.detailTextLabel.text  isEqual: @" "]) {

    cell.detailTextLabel.text = [jsonContent objectAtIndex:indexPath.row];
}




return cell;
}


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

@end

Is my pattern correct? I dont know how I can load the right profile picture for each json/user and sort it by its published date. I just want some help to fix this one in the right way.

0 Answers0