表格视图
1.添加视图View,连接数据源和委托。
控制器头文件
#import <UIKit/UIKit.h>
@interface Simple_TableViewController:UIViewController<UITableViewDdlegate,UITableViewDataSource>
{
NSArry *listData;
}
@property(nonatomic,retain)NSArry *listData;
@end
Simple_TableViewController.m文件
#import "Simple_TableViewController.h"
@implementation Simple_TableViewController
@synthesize listData;
//创建一个要传递给表的数据的数组
-(void)viewDidLoad
{
NSArry *arry=[[NSArry alloc] initWithObjects:@"Sleepy",@"Sneezy",@"Bashful",@"Happy",@"Doc",@"Grumpy",@"Dopey",@"Thorin",@"Dorin",@"Nori",@"Ori",@"Balin",@"Dwalin",@"Fili",@"Kili",@"Oin",@"Floin",@"Bifur",@"Bofur",@"Bombur",nil];
self.listData=arry;
[arry release];
[super viewDidLoad];
}
#pragma mark table view Data Source Methods
//返回组成列表的表分区中的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.listData count];
}
//绘制表视图其中一行
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//NSIndexPath对象可获得一行或一个分区,调用方法
//以下是绘制表单
{
//绘制一个单元
static NSString *SimpleTableIdentifier=@"SimpleTableIdentifier";
UITableViewCell *cel=[tableView dequeueReusableCelWithIdentifier:SimpleTableIdentifier];
//绘制所有表单单元
if(cell==nil)
{
cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SimpleTableIdentifier] autorelease];
}
//indexPath变量获取要显示哪些行信息
NSUInteger row=[indexPath row];
//text显示信息
cell.text=[listData objectAtIndex:row];
//添加图像
UIImage *image=[UIImage imageNamed:@"star.png"];
cell.image=image;
return cell;
}
/*利用委托delegate 处理行是否被选择
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row=[indexPath row];
if(row==0)
return nil;
return indexPath;
}
//看一下是否能够选中第一行(应该不能),然后选择其他行。被选中的行应该会突出显示,然后弹出警告通知你选择的是哪一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row=[indexPath row];
NSString *rowValue=[listData objectAtIndex:row];
NSString *message=[[NSString alloc] initWithFormat:@"You selected %@",rowValue];
UIAertView *alert=[[UIAertView alloc] initWithTitle:@"Row Selected!"
message:message
delegate:nil
cancelButtonTitle:@"Yes I Did"
otherButtonTitles:nil];
[alert show];
[message release];
[alert release];
}
*/
@end