// // MasterViewController.m // Juick // // Created by Vitaly Takmazov on 26.10.13. // Copyright (c) 2013 com.juick. All rights reserved. // #import "MasterViewController.h" #import "MessageCell.h" #import "Message.h" static NSString *CellIdentifier = @"MessageCell"; @interface MasterViewController () { NSMutableArray *messages; } @end @implementation MasterViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"Messages"; // Change button color // Set the gesture messages = [[NSMutableArray alloc] init]; NSError *error; NSURL *url = [NSURL URLWithString:@"http://api.juick.com/messages"]; NSString *data = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; if (data == nil) { NSLog(@"Download Error: %@", error); return; } NSArray *resultObject = [NSJSONSerialization JSONObjectWithData:[data dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error]; if (resultObject == nil) { NSLog(@"JSON Error: %@", error); return; } for (NSDictionary *message in resultObject) { Message *msg = [[Message alloc] initWithDictionary:message]; [messages addObject:msg]; } //UINib *cellNib = [UINib nibWithNibName:@"MessageCell" bundle:nil]; //[self.tableView registerNib:cellNib forCellReuseIdentifier:@"MessageCell"]; [self.tableView registerClass:[MessageCell class] forCellReuseIdentifier:CellIdentifier]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentSizeCategoryChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIContentSizeCategoryDidChangeNotification object:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)contentSizeCategoryChanged:(NSNotification *)notification { [self.tableView reloadData]; } -(void) parserDidEndDocument:(NSXMLParser *)parser { [self.tableView reloadData]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return messages.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; [cell updateFonts]; Message *msg = [messages objectAtIndex:indexPath.row]; cell.titleLabel.text = msg.user; cell.bodyLabel.text = msg.text; [cell setNeedsUpdateConstraints]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; [cell updateFonts]; Message *msg = [messages objectAtIndex:indexPath.row]; cell.titleLabel.text = msg.user; cell.bodyLabel.text = msg.text; cell.bodyLabel.preferredMaxLayoutWidth = tableView.bounds.size.width - (kLabelHorizontalInsets * 2.0f); [cell setNeedsUpdateConstraints]; [cell updateConstraintsIfNeeded]; [cell.contentView setNeedsLayout]; [cell.contentView layoutIfNeeded]; CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; return height; } - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { return 500.0f; } @end