现在的位置: 首页 > 综合 > 正文

iphone练习之TableView

2018年07月27日 ⁄ 综合 ⁄ 共 8507字 ⁄ 字号 评论关闭

1、第一个要实现的效果如图:

新建一个基于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. }  


实现效果图:

转自:http://blog.csdn.net/rhljiayou/article/details/7532440

抱歉!评论已关闭.