基本术语:我们把Table View的一行称为Cell,多个Cell组成一个Section,每个Section上下有Header和Footer,多个Section组成了整个Table ,Table也有Header和Footer。
Table View主要分为以下两种:Plain 普通的列表风格 和 Grouped分块/组风格。
---------------------------------------------------------------------------------------
先做一个普通列表风格的例子:
1、新建项目
打开ViewController.xib文件,往ViewController.xib界面上拖拽一个Table View控件到现有的View上,对齐。
![](https://img-my.csdn.net/uploads/201206/07/1339068343_3074.png)
#import <UIKit/UIKit.h>
@property (strong, nonatomic) NSArray *list;
@end
5、打开.m文件,添加:
@synthesize list = _list;
![](https://img-my.csdn.net/uploads/201206/07/1339068863_2543.png)
这是发现有两个警告,提示未完成的实现,这提示的是UITableViewDelegate, UITableViewDataSource这个两个头文件里的协议的方法未实现。待会我们去实现它。
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *array = [[NSArray alloc] initWithObjects:@"美国", @"菲律宾",
@"黄岩岛", @"中国", @"泰国", @"越南", @"老挝",
@"日本" , nil];
self.list = array;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
self.list = nil;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *array = [[NSArray alloc] initWithObjects:@"美国", @"菲律宾",
@"黄岩岛", @"中国", @"泰国", @"越南", @"老挝",
@"日本" , nil];
self.list = array;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
self.list = nil;
}
7、生成row
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
TableSampleIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:TableSampleIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [self.list objectAtIndex:row];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
TableSampleIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:TableSampleIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [self.list objectAtIndex:row];
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TableSampleIdentifier];
这个语句根据标识符TableSampleIdentifier寻找当前可以重用的UITableViewCell。当某行滑出当前可见区域后,我们重用它所对应的UITableViewCell对象,那么就可以节省内存和资源。
注意参数(NSIndexPath *)indexPath,它将行号row和部分号section合并了,通过[indexPath row];获取行号。之后给cell设置其文本:
cell.textLabel.text = [self.list objectAtIndex: row];
8、现在一个简单的TableView就弄好看,运行下看效果
cell.textLabel.text = [self.list objectAtIndex:row];
UIImage *image = [UIImage imageNamed:@"qq"];
cell.imageView.image = image;
UIImage *highLighedImage = [UIImage imageNamed:@"youdao"];
cell.imageView.highlightedImage = highLighedImage;
return cell;
NSUInteger row = [indexPath row];
cell.textLabel.text = [self.list objectAtIndex:row];
UIImage *image = [UIImage imageNamed:@"qq"];
cell.imageView.image = image;
UIImage *highLighedImage = [UIImage imageNamed:@"youdao"];
cell.imageView.highlightedImage = highLighedImage;
return cell;
效果如下:
UITableViewCellStyleDefault
UITableViewCellStyleSubtle
UITableViewCellStyleValue1
UITableViewCellStyleValue2
cell.detailTextLabel.text =@"打打打打";
return cell;
![](https://img-my.csdn.net/uploads/201206/07/1339071659_5425.png)
![](https://img-my.csdn.net/uploads/201206/08/1339116634_3341.png)
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
选中是做个提示,提示选中了那个信息,代码实现如下:
NSString *rowString = [self.list objectAtIndex:[indexPath row]];
UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"选中的行信息" message:rowString delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alter show];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *rowString = [self.list objectAtIndex:[indexPath row]];
UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"选中的行信息" message:rowString delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alter show];
}
效果:
难是难在如何凑成那些暂时数据。