iphone练习之TableView
来源:互联网 发布:linux镜像文件下载 编辑:程序博客网 时间:2024/05/22 03:49
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];}
实现效果图:
本博客是我自己的练习,有好多地方没有讲太清楚,还请谅解!
- iphone练习之TableView
- iphone练习之TableView
- iphone练习之TableView
- iPhone开发之TableView
- iphone开发之TableView控件实例
- [iphone]tableview
- iPhone开发【七】常用控件之表TableView
- iPhone开发【七】常用控件之表TableView
- iPhone学习之路 TableView实现动态加载图片
- iphone tableView的使用
- Iphone TableView 使用实现
- iphone多线程练习之火车售票模拟
- iPhone tableview分批显示数据
- iphone tableView的使用(zz)
- iphone的tableview滑动删除
- iphone练习之实现图片切换(用代码实现)
- iphone第九节:界面练习
- iPhone TableView中图片的延时加载
- oracle flashback详解2
- OpenGL ES系列之0 – 建立Xcode项目
- Oracle数据库Exp/Imp工具性能调优
- libgdx game 管理场景 stage 管理actor 以及照相机的demo
- 安装织梦
- iphone练习之TableView
- 公司培训记
- KGB SFX 脱壳
- iphone-OpenGL ES 教程-01 --西蒙
- iphone-OpenGL ES 教程-02 --西蒙
- gridview滚动条
- iphone-OpenGL ES 教程-03--西蒙
- g729源码分析-7-增益量化
- 数据库事物隔离级别