¶ÁÊéÈË

¶ÁÈ¡Movies.plist×ÊÁϲ¢ÏÔʾµ½table V

·¢²¼Ê±¼ä£º 2013-09-29 11:07:08 ×÷Õߣº rapoo

¶ÁÈ¡Movies.plistÎļþ²¢ÏÔʾµ½table ViewÖÐ

н¨Ò»¸öMovies.plistÎļþ

¶ÁÈ¡Movies.plist×ÊÁϲ¢ÏÔʾµ½table ViewÖÐ
















¹Ø¼ü´úÂ룺

ViewController.h

#import <UIKit/UIKit.h>


@interface ViewController : UIViewController<UITableViewDataSource,UITabBarDelegate>

@property(nonatomic,retain) NSDictionary *moveTitles;

@property(nonatomic,retain) NSArray *years;

@end


ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad

{

[super viewDidLoad];

//path to the property list file

NSString *path = [[NSBundle mainBundle] pathForResource:@"Movies" ofType:@"plist"];

//load the list into the dictionary

self.moveTitles = [[NSDictionary alloc] initWithContentsOfFile:path];

//get all the keys in the dictionary object and sort them

self.years = [[self.moveTitles allKeys] sortedArrayUsingSelector:@selector(compare:)];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return [self.years count];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

//check the current year based on the section index

NSString *year = [self.years objectAtIndex:section];

//returns the movies in that year as an array

NSArray *moviesSection = [self.moveTitles objectForKey:year];

return [moviesSection count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;

}

//get the year

NSString *year = [self.years objectAtIndex:[indexPath section]];

//get the list of movies for that year

NSArray *moviesSection = [self.moveTitles objectForKey:year];

//get the particular movies based on that row

cell.textLabel.text = [moviesSection objectAtIndex:[indexPath row]];


// Configure the cell...

return cell;

}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

//get the year as the section header

NSString *year = [self.years objectAtIndex:section];

return year;

}

@end


ÔËÐнá¹û£º

¶ÁÈ¡Movies.plist×ÊÁϲ¢ÏÔʾµ½table ViewÖÐ

















Ô­ÎÄ£ºhttp://chaoyuan.sinaapp.com/?p=451








¶ÁÊéÈËÍø >ÒÆ¶¯¿ª·¢

ÈȵãÍÆ¼ö