Monday, 23 February 2015

Property List

Property List (PList)


1) add Property List in The Project :



2) add This Type of Data in PList :

When prompted, use “recipes” as the file name. Once you confirm, Xcode will create the property list file for you. By default, the property list is empty.



There are two ways to edit the property list. You can right-click on the editing area and select “Add Row” to add a new value.
Xcode Property List Add Row
Add a New Row in Property List Editor
As we’re going to put the three data arrays in the property list, we’ll add three rows with “array” type. Name them with the keys: RecipeName, Thumbnail and PrepTime. The key serves as an identifier and later you’ll use it in your code to pick the corresponding array.
SimpleTableApp Property List Array
Define Three Arrays in Property List
To add data in the array, just expand it and click the “+” icon to add a new item. Follow the steps in the below illustration if you don’t know how to do it.
Add Property in Property List
Step by Step Procedures to Add an Item in Array
Repeat the procedures until you add all the values for the array. Your property list should look like this:
SimpleTableApp Recipe Property List
Recipe Property List
For your convenience, you may download the recipes.plist and add it to your project.
As mentioned earlier, the property list is usually saved in the format of XML. To view the source of the property list, right click and select “Open as Source Code”.
Open Property List as Source Code
View the Source Code of Property List
The source code of “recipes.plist” file will appear like this:
Property List Source Code
Source Code of Recipes.plist

3) Write Coding Which is Given Below :

Main.Storyboard :



ViewController.h :

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property(strong,nonatomic)IBOutlet UITableView *tblobj;

@property(strong,nonatomic)NSMutableArray *arrrecipe;
@property(strong,nonatomic)NSMutableArray *arrthumbail;
@property(strong,nonatomic)NSMutableArray *arrtime;

@end

ViewController.m :


#import "ViewController.h"
#import "CustomCell.h"




@interface ViewController ()

@end

@implementation ViewController
@synthesize arrrecipe,arrthumbail,arrtime,tblobj;

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"reciepy" ofType:@"plist"];
    
    NSDictionary *dec = [[NSDictionary alloc]initWithContentsOfFile:path];
    
    arrrecipe = [dec objectForKey:@"RecipeName"];
    arrthumbail = [dec objectForKey:@"Thumbail"];
    arrtime = [dec objectForKey:@"PreTime"];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arrrecipe.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [tblobj dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.objdata.image = [UIImage imageNamed:[arrthumbail objectAtIndex:indexPath.row]];
    cell.namedata.text = [arrrecipe objectAtIndex:indexPath.row];
    cell.timedata.text = [arrtime objectAtIndex:indexPath.row];
    
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Select" message:[arrrecipe objectAtIndex:indexPath.row] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    
    [alert show];
    
    
}

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

@end


CustomCell.h :

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell

@property(strong,nonatomic)IBOutlet UIImageView *objdata;
@property(strong,nonatomic)IBOutlet UILabel *namedata;
@property(strong,nonatomic)IBOutlet UILabel *timedata;

@end

CustomCell.m :

#import "CustomCell.h"

@implementation CustomCell
@synthesize objdata,namedata,timedata;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end


No comments:

Post a Comment