iphone练习之TableView
新建一个基于Sigle view Application的项目,拖一个Table View到View上,实现Outlets:dataSource、delegate到File's Owner。
实现代码:
#import <UIKit/UIKit.h>
//为了填充表格,必须使用一个协议,并且实现协议中的两个方法
@interface ViewController : UIViewController<UITableViewDataSource>
@end
#import <UIKit/UIKit.h>
//为了填充表格,必须使用一个协议,并且实现协议中的两个方法
@interface ViewController : UIViewController<UITableViewDataSource>
@end
[cpp] #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];
}
#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 <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
[cpp] #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];
}
#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添加一个索引方法如下代码:
[cpp] //有时候列表过长,添加此方法实现索引,按每一年索引
-(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];
}
- (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文件中添加如下信息:
[cpp] #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
#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文件中添加:
[cpp] @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];
}
@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];
}
实现效果图:
摘自 任海丽(3G/移动开发)
相关新闻>>
- 发表评论
-
- 最新评论 更多>>