#import "FirstViewController.h"
#import "SingleViewController.h"
#import "CrotuneDemoAppDelegate.h"
#import "CellView.h"
@implementation FirstViewController
@synthesize tableDataSource,CurrentTitle,CurrentLevel,navigationController,reklama,mreklama,stringMreklama,stringReklama;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
//
if (CurrentLevel==0) {
//start datasource
NSArray *tempArray = [[NSArray alloc] init];
self.tableDataSource=tempArray;
[tempArray release];
CrotuneDemoAppDelegate *AppDelegate = (CrotuneDemoAppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource = [AppDelegate.data objectForKey:@"Rows"];
self.navigationItem.title = @"Categories";
[mreklama setImage:[UIImage imageNamed:@"mkras.jpg"]];
[reklama setImage:[UIImage imageNamed:@"kras.jpg"]];
}
else{
self.navigationItem.title = CurrentTitle;
[mreklama setImage:[UIImage imageNamed:stringMreklama]];
[reklama setImage:[UIImage imageNamed:stringReklama]];
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:1];
[UIView setAnimationDuration:1];
[reklama setAlpha:0];
[UIView commitAnimations];
//NSLog(@"%@",tableDataSource);
}
//table delegate metods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.tableDataSource count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CellView";
CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CellView" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[CellView class]])
{
cell = (CellView *)currentObject;
break;
}
}
}
// Set up the cell...
NSDictionary *dictionary =[self.tableDataSource objectAtIndex:indexPath.row];
[[cell title] setText:[dictionary objectForKey:@"Title"]];
[[cell background] setImage:[UIImage imageNamed:[dictionary objectForKey:@"Image"]]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Get the dictionary of the selected data source.
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
//Get the children of the present item.
NSArray *Children = [dictionary objectForKey:@"Children"];
if([Children count] == 0) {
SingleViewController *detailView =[[SingleViewController alloc] initWithNibName:@"SingleView" bundle:[NSBundle mainBundle]];
//ovdje dodaj objekt sa podatcima od pojedinog subjekta
[self.navigationController pushViewController:detailView animated:YES];
[detailView release];
}
else {
//Prepare to tableview.
FirstViewController *nextView = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:[NSBundle mainBundle]];
//Increment the Current View
nextView.CurrentLevel += 1;
//Set the title;
nextView.CurrentTitle = [dictionary objectForKey:@"Title"];
nextView.stringMreklama=[dictionary objectForKey:@"SAdd"];
nextView.stringReklama=[dictionary objectForKey:@"LAdd"];
nextView.tableDataSource = Children;
//Push the new table view on the stack
[self.navigationController pushViewController:nextView animated:YES];
[nextView release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end