1、第一個要實現的效果
建立一個基於Sigle view Application的項目,拖一個Table View到View上,實現Outlets:dataSource、delegate到File's Owner。
實現代碼:
#import <UIKit/UIKit.h>//為了填充表格,必須使用一個協議,並且實現協議中的兩個方法@interface ViewController : UIViewController<UITableViewDataSource>@end
#import "ViewController.h"@implementation ViewControllerNSMutableArray *listOfMovies;//設定table中的資訊,行的儲存格在索引路徑-(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]; } //設定一行cell顯示的值 NSString *cellValue=[listOfMovies objectAtIndex:indexPath.row]; cell.textLabel.text=cellValue; //添加圖片 UIImage *image=[UIImage imageNamed:@"ic_ic.jpg"]; cell.imageView.image=image; return cell;}//節的行數-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [listOfMovies count];}-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ //顯示頁首 return @"Movie List";}-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ //顯示頁尾 return @"by Denzel Washington";}//選擇在指數徑行-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //得到選中該行的內容 NSString *movieSelected=[listOfMovies objectAtIndex:indexPath.row]; //封裝成msg NSString *msg=[NSString stringWithFormat:@"You have selected %@",movieSelected]; //用警告框彈出 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Movie selected" message:msg delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil]; //顯示彈出對話方塊 [alert show]; //釋放alert [alert release];}//縮排水平排在索引路徑-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{ return [indexPath row]%2;}//在索引路徑為行高度-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 70;}- (void)viewDidLoad{ listOfMovies=[[NSMutableArray alloc]init]; [listOfMovies addObject:@"Training Day"]; [listOfMovies addObject:@"Remember the Titans"]; [listOfMovies addObject:@"John Q."]; [listOfMovies addObject:@"The Bone Collector"]; [listOfMovies addObject:@"Ricochet"]; [listOfMovies addObject:@"The Siege"]; [listOfMovies addObject:@"Malcolm X"]; [listOfMovies addObject:@"Antwone Fisher"]; [listOfMovies addObject:@"Courage Under Fire"]; [listOfMovies addObject:@"He Got Game"]; [listOfMovies addObject:@"The Pelican Brief"]; [listOfMovies addObject:@"Glory"]; [listOfMovies addObject:@"The Preacher’s Wife"]; [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}-(void)dealloc{ [listOfMovies release]; [super dealloc];}
我只給出相應的方法實現!
2、第二種實現效果
建立一個基於Master-Detail Application;在檔案裡建立一個Property List類型的檔案名稱為Movies.plist,內容如下:
實現代碼:
#import <UIKit/UIKit.h>@class DetailViewController;@interface MasterViewController : UITableViewController{ NSDictionary *movieTitles; NSArray *years;}@property (nonatomic,retain)NSDictionary *movieTitles;@property (nonatomic,retain)NSArray *years;@property (strong, nonatomic) DetailViewController *detailViewController;@end
#import "MasterViewController.h"#import "DetailViewController.h"@implementation MasterViewController@synthesize movieTitles,years;- (void)dealloc{ [_detailViewController release]; [movieTitles release]; [years release]; [super dealloc];}- (void)viewDidLoad{ //檔案名稱字及類型 NSString *path=[[NSBundle mainBundle]pathForResource:@"Movies" ofType:@"plist"]; //擷取內容為字典類型 NSDictionary *dic=[[NSDictionary alloc]initWithContentsOfFile:path]; //把所有內容賦給movieTitles self.movieTitles=dic; [dic release]; /*擷取所有的年份,並且升序鍵 2000, 2001, 2002, 2004, 2006, 2007, 2008*/ NSArray *array=[[self.movieTitles allKeys]sortedArrayUsingSelector:@selector(compare:)]; //賦給數組年 self.years=array; [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ //返回多少總行 return [self.years count];}//每節的行數-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ //擷取每一年 NSString *year=[self.years objectAtIndex:section]; //擷取每個年裡的值,得到一個數組 NSArray *movieSection=[self.movieTitles objectForKey:year]; //返回這個鍵總共有多少值 return [movieSection count];}//添寫每一節的內容-(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]; } //擷取每一年 NSString *year=[self.years objectAtIndex:[indexPath section]]; //擷取每年裡的值 NSArray *movieSection=[self.movieTitles objectForKey:year]; //設定每一節裡的內容 cell.textLabel.text=[movieSection objectAtIndex:[indexPath row]]; return cell;}//年的頁首-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ NSString *year=[self.years objectAtIndex:section]; return year;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (!self.detailViewController) { self.detailViewController = [[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil] autorelease]; } [self.navigationController pushViewController:self.detailViewController animated:YES];}
開啟MasterViewController.xib檔案把Table View的屬性Style改成Grouped,並在MasterViewController.m添加一個索引方法如下代碼:
//有時候列表過長,添加此方法實現索引,按每一年索引-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{ return years;}
實現的效果如:
下面是切換到另一個節目,並把電影的名字帶回去:
首先在DetailViewController.m檔案中添加如入代碼:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *year = [self.years objectAtIndex:[indexPath section]]; NSArray *movieSection = [self.movieTitles objectForKey:year]; NSString *movieTitle = [movieSection objectAtIndex:[indexPath row]]; NSString *message = [[NSString alloc]initWithFormat:@"%@", movieTitle]; if (!self.detailViewController) { self.detailViewController = [[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil] autorelease]; } self.detailViewController.movieSelected=message; [self.navigationController pushViewController:self.detailViewController animated:YES];}
在DetailViewController.xib檔案中添加一個label;
在DetailViewController.h檔案中添加如下資訊:
#import <UIKit/UIKit.h>@interface DetailViewController : UIViewController{ NSString *movieSelected;//電影的名字 IBOutlet UILabel *label;}@property (strong, nonatomic) id detailItem;@property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel;@property (nonatomic,retain)NSString *movieSelected;@property (nonatomic,retain)IBOutlet UILabel *label;@end
在DetailViewController.m檔案中添加:
@interface DetailViewController ()- (void)configureView;@end@implementation DetailViewController@synthesize detailItem = _detailItem;@synthesize detailDescriptionLabel = _detailDescriptionLabel;@synthesize movieSelected,label;- (void)dealloc{ [_detailItem release]; [_detailDescriptionLabel release]; [movieSelected release]; [super dealloc];}- (void)viewDidLoad{ self.navigationItem.title = movieSelected; label.text=movieSelected; [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. [self configureView];}
實現:
本部落格是我自己的練習,有好多地方沒有講太清楚,還請諒解!