Popular Posts

May 23, 2011

Show Data In Table View Using Plist


I show how to use arrays to show data. Today i have use plist to show the data.plist means property list.
- Create a plist
-The root type will be array.
-Then add  row.make the type Dictionary.
-Then add new row…and make their type string.


//
//  RootViewController.h
//  iTableView
//
//  Created by Jubayer Ahmed on 5/22/11.
//  Copyright __MyCompanyName__ 2011. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController {
NSMutableArray *datas;
}
@property(nonatomic, retain) NSMutableArray *datas;

@end



//
//  RootViewController.m
//  iTableView
//
//  Created by Jubayer Ahmed on 5/22/11.
//  Copyright __MyCompanyName__ 2011. All rights reserved.
//

#import "RootViewController.h"
#import "DataView.h"

@implementation RootViewController
@synthesize datas;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
         self.title = @"Data";
         NSString *path = [[NSBundle mainBundle] pathForResource:@"dataList" ofType:@"plist"];
         NSMutableArray *tempArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
        
//       NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects:@"Dhaka",@"Chittagonj",@"ManikGonj",nil];
     self.datas = tempArray;
          [tempArray release];
        
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
         [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
         [super viewDidDisappear:animated];
}
*/

/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
         // Return YES for supported orientations.
         return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 */


#pragma mark -
#pragma mark Table view data source

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.datas count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
   
         // Configure the cell.
         //cell.textLabel.text = [self.datas objectAtIndex:indexPath.row];
         cell.textLabel.text = [[self.datas objectAtIndex:indexPath.row]objectForKey:@"name"];
         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
         return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }  
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }  
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  
         DataView *dataViews = [[DataView alloc] initWithNibName:@"DataView" bundle:nil];
         dataViews.dataMutableDictionary = [self.datas objectAtIndex:indexPath.row];
         [self.navigationController pushViewController:dataViews animated:YES];
    [dataViews release];

        
         /*
          <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
          [self.navigationController pushViewController:detailViewController animated:YES];
          [detailViewController release];
          */
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
   
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
         [datas release];
}


@end

- Now add UIViewControllersubclass with XIB file.


//
//  DataView.h
//  iTableView
//
//  Created by Jubayer Ahmed on 5/22/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface DataView : UIViewController {
         NSDictionary *data;
         NSMutableDictionary *dataMutableDictionary;
         IBOutlet UILabel  *nameLabel;
         IBOutlet UILabel  *phoneLabel;
}
@property(nonatomic,retain) NSDictionary *data;
@property(nonatomic, retain) NSMutableDictionary *dataMutableDictionary;
@property(nonatomic, retain) IBOutlet UILabel  *nameLabel;
@property(nonatomic, retain) IBOutlet UILabel  *phoneLabel;
@end



//
//  DataView.m
//  iTableView
//
//  Created by Jubayer Ahmed on 5/22/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DataView.h"


@implementation DataView
@synthesize data, dataMutableDictionary;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
         self.title = @"Detail";
}

-(void) viewWillAppear:(BOOL)animated{
         [super viewWillAppear:animated];
         nameLabel.text = [self.dataMutableDictionary objectForKey:@"name"];
         phoneLabel.text = [self.dataMutableDictionary objectForKey:@"Phone"];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (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 {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end





2 comments: