XCode很多操作都直接是拖动鼠标就可完成,虽然定义UI的xib文件也是xml格式,但是用文本编辑器打开看了以后,还是相当复杂的,要直接写code来做UI还是有点问题。这样有利也有弊,利是做UI更简单了,弊是在我没有完全理解iOS的机制之前,必须记住每个用鼠标操作的步骤。所以,笔记还是做得详细一点,tableView的使用会分成几篇。 iOS中的tableView就是android中的ListView,同样显示一列数据。下面的例子是最简单的,效果如图: 步骤: 新建一项目—Single View Application—输入Product Name,Company Identifier,Class Prefix,Device Family选iPhone,只勾Use Automatic Reference Counting—Create. 我的Product Name:Simple Table, Class Prefix:PD 随便导入一张图片,我这文件名为57.png,拖动图片到项目上就行,好像位置要求不严格,但还是建议自己建个images的Group 选中PDViewController.xib,以打开Interface Builder,右下角Objects里找到Table View,拖放到UI上,会全屏覆盖两个界面。选中刚添加的Table View,打开Connections inspector,发现有两个Outlets,dataSource和delegate,拖动右边小圆点到左侧File’s Owner上。 下面开始都是代码了。 PDViewController.h:
#import
@interface PDViewController : UIViewController
//添加UITableViewDelegate,UITableViewDataSource协议
@property (strong,nonatomic) NSArray * listData;
//存储列表数据
@end
PDViewController.m:
#import "PDViewController.h"
@implementation PDViewController
@synthesize listData=_listData;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
_listData=[NSArray arrayWithObjects:@"aaa",@"bbb",@"ccc",@"ddd",
@"eee",@"fff",@"ggg",@"hhh",@"iii",@"jjj",@"kkk",@"lll",@"mmm",@"nnn",@"ooo",nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
_listData=nil;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
//下面都是实现UITableViewDelegate,UITableViewDataSource两个协议中定义的方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//返回行数
return [_listData count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//返回一行的视图
NSUInteger row=[indexPath row];
NSString * tableIdentifier=@"Simple table";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tableIdentifier];
//当一行上滚在屏幕消失时,另一行从屏幕底部滚到屏幕上,如果新行可以直接使用已经滚出屏幕的那行,系统可以避免重新创建和释放视图,同一个TableView,所有的行都是可以复用的,tableIdentifier是用来区别是否属于同一TableView
if(cell==nil)
{
//当没有可复用的空闲的cell资源时(第一次载入,没翻页)
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
//UITableViewCellStyleDefault 只能显示一张图片,一个字符串,即本例样式
//UITableViewCellStyleSubtitle 可以显示一张图片,两个字符串,上面黑色,下面的灰色
//UITableViewCellStyleValue1 可以显示一张图片,两个字符串,左边的黑色,右边的灰色
//UITableViewCellStyleValue2 可以显示两个字符串,左边的灰色,右边的黑色
}
cell.textLabel.text=[_listData objectAtIndex:row];//设置文字
UIImage *image=[UIImage imageNamed:@"57"];//读取图片,无需扩展名
cell.imageView.image=image;//文字左边的图片
// cell.detailTextLabel.text=@"详细描述"; 适用于Subtitle,Value1,Value2样式
// cell.imageView.highlightedImage=image; 可以定义被选中后显示的图片
return cell;
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{//被选中前执行
if ([indexPath row]==0) {
//第一项不可选
return nil;
}
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{//被选中后执行
NSString * string=[_listData objectAtIndex:[indexPath row]];
//取出被选中项的文字
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:string delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//返回每行的高度
//CGFloat就是float
return 70.0;
}
/*
其他可能常用的方法:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
以上分别返回table头和尾的高度
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
上面两方法可以自定义table头和尾
*/
@end