新建一個基於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 ViewController
- NSMutableArray *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];
- }