iphone练习之TableView
1、第一个要实现的效果如图:新建一个基于Sigle view Application的项目,拖一个Table View到View上,实现Outlets:dataSource、delegate到File's Owner。
实现代码:
#import <UIKit/UIKit.h>
//为了填充表格,必须使用一个协议,并且实现协议中的两个方法
@inte易做图ce ViewController : UIViewController<UITableViewDataSource>
@end
#import <UIKit/UIKit.h>
//为了填充表格,必须使用一个协议,并且实现协议中的两个方法
@inte易做图ce 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=[[UI
补充:移动开发 , IOS ,