iphone練習之TableView

新建一個基於Sigle view Application的項目,拖一個Table View到View上,實現Outlets:dataSource、delegate到File's Owner。

實現代碼:

  1. #import <UIKit/UIKit.h>  
  2. //爲了填充表格,必須使用一個協議,並且實現協議中的兩個方法  
  3. @interface ViewController : UIViewController<UITableViewDataSource>  
  4.   
  5. @end  

  1. #import "ViewController.h"  
  2.   
  3. @implementation ViewController  
  4. NSMutableArray *listOfMovies;  
  5. //設置table中的信息,行的單元格在索引路徑  
  6. -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  7.       
  8.     static NSString *CellIdentifier=@"Cell";  
  9.     //設置重複用的電池  
  10.     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  11.     if(cell==nil){  
  12.         cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];  
  13.           
  14.     }  
  15.     //設置一行cell顯示的值  
  16.     NSString *cellValue=[listOfMovies objectAtIndex:indexPath.row];  
  17.     cell.textLabel.text=cellValue;  
  18.     //添加圖片  
  19.     UIImage *image=[UIImage imageNamed:@"ic_ic.jpg"];  
  20.     cell.imageView.image=image;  
  21.     return cell;  
  22. }  
  23. //節的行數  
  24. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  25.     return [listOfMovies count];  
  26. }  
  27. -(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{  
  28.     //顯示頁眉  
  29.     return @"Movie List";  
  30. }  
  31. -(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{  
  32.     //顯示頁腳  
  33.     return @"by Denzel Washington";  
  34. }  
  35. //選擇在指數徑行  
  36. -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  37.     //得到選中該行的內容  
  38.     NSString *movieSelected=[listOfMovies objectAtIndex:indexPath.row];  
  39.     //封裝成msg  
  40.     NSString *msg=[NSString stringWithFormat:@"You have selected %@",movieSelected];  
  41.     //用警告框彈出  
  42.     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Movie selected" message:msg delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];  
  43.     //顯示彈出對話框  
  44.     [alert show];  
  45.     //釋放alert  
  46.     [alert release];  
  47. }  
  48. //縮進水平排在索引路徑  
  49. -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{  
  50.     return [indexPath row]%2;  
  51. }  
  52. //在索引路徑爲行高度  
  53. -(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
  54.     return 70;  
  55. }  
  56. - (void)viewDidLoad  
  57. {  
  58.     listOfMovies=[[NSMutableArray alloc]init];  
  59.     [listOfMovies addObject:@"Training Day"];  
  60.     [listOfMovies addObject:@"Remember the Titans"];  
  61.     [listOfMovies addObject:@"John Q."];   
  62.     [listOfMovies addObject:@"The Bone Collector"];  
  63.     [listOfMovies addObject:@"Ricochet"];   
  64.     [listOfMovies addObject:@"The Siege"];   
  65.     [listOfMovies addObject:@"Malcolm X"];  
  66.     [listOfMovies addObject:@"Antwone Fisher"];   
  67.     [listOfMovies addObject:@"Courage Under Fire"];  
  68.     [listOfMovies addObject:@"He Got Game"];   
  69.     [listOfMovies addObject:@"The Pelican Brief"];   
  70.     [listOfMovies addObject:@"Glory"];  
  71.     [listOfMovies addObject:@"The Preacher’s Wife"];  
  72.     [super viewDidLoad];  
  73.     // Do any additional setup after loading the view, typically from a nib.  
  74. }  
  75. -(void)dealloc{  
  76.     [listOfMovies release];  
  77.     [super dealloc];  
  78. }  

我只給出相應的方法實現!

2、第二種實現效果


新建一個基於Master-Detail Application;在文件裏新建一個Property List類型的文件名爲Movies.plist,內容如下:


實現代碼:

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @class DetailViewController;  
  4.   
  5. @interface MasterViewController : UITableViewController{  
  6.     NSDictionary *movieTitles;  
  7.     NSArray *years;  
  8. }  
  9. @property (nonatomic,retain)NSDictionary *movieTitles;  
  10. @property (nonatomic,retain)NSArray *years;  
  11. @property (strong, nonatomic) DetailViewController *detailViewController;  
  12.   
  13. @end  

  1. #import "MasterViewController.h"  
  2. #import "DetailViewController.h"  
  3.   
  4. @implementation MasterViewController  
  5. @synthesize movieTitles,years;  
  6.   
  7. - (void)dealloc  
  8. {  
  9.     [_detailViewController release];  
  10.     [movieTitles release];  
  11.     [years release];  
  12.     [super dealloc];  
  13. }  
  14.   
  15. - (void)viewDidLoad  
  16. {  
  17.     //文件名字及類型  
  18.     NSString *path=[[NSBundle mainBundle]pathForResource:@"Movies" ofType:@"plist"];  
  19.     //獲取內容爲字典類型  
  20.     NSDictionary *dic=[[NSDictionary alloc]initWithContentsOfFile:path];  
  21.     //把所有內容賦給movieTitles  
  22.     self.movieTitles=dic;  
  23.     [dic release];  
  24.     /*獲取所有的年份,並且升序鍵 
  25.      2000, 
  26.     2001, 
  27.     2002, 
  28.     2004, 
  29.     2006, 
  30.     2007, 
  31.     2008*/      
  32.     NSArray *array=[[self.movieTitles allKeys]sortedArrayUsingSelector:@selector(compare:)];  
  33.     //賦給數組年  
  34.     self.years=array;      
  35.     [super viewDidLoad];  
  36.     // Do any additional setup after loading the view, typically from a nib.  
  37. }  
  38. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{  
  39.     //返回多少總行  
  40.     return [self.years count];  
  41. }  
  42. //每節的行數  
  43. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  44.     //獲取每一年  
  45.     NSString *year=[self.years objectAtIndex:section];  
  46.     //獲取每個年裏的值,得到一個數組  
  47.     NSArray *movieSection=[self.movieTitles objectForKey:year];  
  48.     //返回這個鍵總共有多少值  
  49.     return [movieSection count];  
  50. }  
  51. //添寫每一節的內容  
  52. -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  53.       
  54.     static NSString *CellIdentifier = @"Cell";  
  55.       
  56.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];              
  57.     if(cell==nil){  
  58.         cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];  
  59.     }  
  60.     //獲取每一年  
  61.     NSString *year=[self.years objectAtIndex:[indexPath section]];  
  62.    //獲取每年裏的值  
  63.     NSArray *movieSection=[self.movieTitles objectForKey:year];  
  64.    //設置每一節裏的內容  
  65.     cell.textLabel.text=[movieSection objectAtIndex:[indexPath row]];  
  66.     return cell;  
  67. }  
  68. //年的頁眉  
  69. -(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{  
  70.     NSString *year=[self.years objectAtIndex:section];  
  71.     return year;  
  72. }  
  73.   
  74. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  75. {  
  76.     if (!self.detailViewController) {  
  77.         self.detailViewController = [[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil] autorelease];  
  78.     }  
  79.     [self.navigationController pushViewController:self.detailViewController animated:YES];  
  80. }  


打開MasterViewController.xib文件把Table View的屬性Style改成Grouped,並在MasterViewController.m添加一個索引方法如下代碼:

  1. //有時候列表過長,添加此方法實現索引,按每一年索引  
  2. -(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{  
  3.     return years;  
  4. }  
實現的效果如下圖:


下面是切換到另一個節目,並把電影的名字帶回去:

首先在DetailViewController.m文件中添加如入代碼:

  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     NSString *year = [self.years objectAtIndex:[indexPath section]];  
  4.     NSArray *movieSection = [self.movieTitles objectForKey:year];  
  5.     NSString *movieTitle = [movieSection objectAtIndex:[indexPath row]];  
  6.     NSString *message = [[NSString alloc]initWithFormat:@"%@", movieTitle];  
  7.       
  8.     if (!self.detailViewController) {  
  9.         self.detailViewController = [[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil] autorelease];  
  10.     }  
  11.     self.detailViewController.movieSelected=message;  
  12.     [self.navigationController pushViewController:self.detailViewController animated:YES];  
  13. }  


在DetailViewController.xib文件中添加一個label;

在DetailViewController.h文件中添加如下信息:

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface DetailViewController : UIViewController{  
  4.     NSString *movieSelected;//電影的名字  
  5.     IBOutlet UILabel *label;  
  6. }  
  7.   
  8. @property (strong, nonatomic) id detailItem;  
  9.   
  10. @property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel;  
  11. @property (nonatomic,retain)NSString *movieSelected;  
  12.   
  13. @property (nonatomic,retain)IBOutlet UILabel *label;  
  14.   
  15. @end  

在DetailViewController.m文件中添加:

  1. @interface DetailViewController ()  
  2. - (void)configureView;  
  3. @end  
  4.   
  5. @implementation DetailViewController  
  6.   
  7. @synthesize detailItem = _detailItem;  
  8. @synthesize detailDescriptionLabel = _detailDescriptionLabel;  
  9. @synthesize movieSelected,label;  
  10. - (void)dealloc  
  11. {  
  12.     [_detailItem release];  
  13.     [_detailDescriptionLabel release];  
  14.     [movieSelected release];  
  15.     [super dealloc];  
  16. }  
  17.   
  18. - (void)viewDidLoad  
  19. {  
  20.     self.navigationItem.title = movieSelected;  
  21.     label.text=movieSelected;  
  22.     [super viewDidLoad];  
  23.     // Do any additional setup after loading the view, typically from a nib.  
  24.     [self configureView];  
  25. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章