I have a UITableViewController class, and i download the code in viewDidLoad method. Is it okay with that code? I'm not really sure about downloading and displaying content. Because it takes a lot of time to display news, and the table scrolling is lagging. Sorry, i'm new to objective C.
@implementation NewsViewController
- (void)viewDidLoad
{
// _sidebarButton.tintColor = [UIColor colorWithWhite:0.96f alpha:0.2f];
_sidebarButton.target = self.revealViewController;
_sidebarButton.action = @selector(revealToggle:);
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
[super viewDidLoad];
[self getJSON];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return currentCellsCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CellForNews *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
id tempObject=[self.arrayOfNews objectAtIndex:indexPath.row];
cell.publishDate.text=tempObject[@"publish_date"];
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:tempObject[@"img_path"]]];
cell.newsImage.image=[UIImage imageWithData:data];
cell.descriptionOfThenews.text=tempObject[@"body"];
cell.titleOfTheNews.text=tempObject[@"publish_title"];
return cell;
}
-(void)getJSON{
NSString *path=@"example.com";
NSURL *url=[NSURL URLWithString:path];
NSString *dataJSON=[NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:Nil];
NSData *data=[dataJSON dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *rootDictionary=[NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:nil];
NSDictionary *newsDict=[rootDictionary objectForKey:@"publications"];
id marker=[[NSObject alloc]init];
self.arrayOfNews=(NSMutableArray*)[newsDict objectsForKeys:[newsDict allKeys] notFoundMarker:marker];
currentCellsCount=[newsDict allKeys].count;
}