iOS开发之高级视图—— UITableView(一)简单例子
来源:互联网 发布:游戏源码是什么 编辑:程序博客网 时间:2024/05/22 02:16
表视图继承自UIScrollView,这样的继承关系使得表视图可以实现上、下滚动。
UITableView需要实现的两个协议如下:
UITableViewDatasource:实例化表视图时,必须采用该方法来实现数据源的配置 UITableViewDelegate:表视图的委托方法,一般用于处理表视图的基本样式以及捕捉选中单元格选中事件 表视图的结构: 表视图由头部、尾部视图,中间有一连串的单元格视图 表视图的头部由tableHeaderView属性设置,尾部视图通过tableFooterView属性设置 分组表格由一系列的 分区 视图组成,每一个分区又包含一个连续的单元格 每个分区视图也由头部视图和尾部视图,通过委托方法代理 cell的使用: 首先定义一个标示符 其次,检查表视图中是否存在闲置的单元格,如果有取出来,没有则重新创建
例子一——简单表格
ViewController.m
-
-
-
-
-
-
-
-
- #import "ViewController.h"
-
- @interface ViewController ()
-
- @end
-
- NSArray* cityList;
-
- @implementation ViewController
-
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- [self.view setBackgroundColor:[UIColor redColor]];
-
-
- cityList = @[@"北京",@"上海",@"天津",@"广州",@"深圳",@"杭州",@"长沙",@"郴州"];
-
- UITableView* tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
-
- UILabel* headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0)];
- [headerLabel setText:@"城市列表"];
- headerLabel.textColor = [UIColor blackColor];
- headerLabel.backgroundColor = [UIColor cyanColor];
- headerLabel.textAlignment = NSTextAlignmentCenter;
-
- [tableView setTableHeaderView:headerLabel];
-
- UILabel* footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
- [footerLabel setText:@"以上城市房价太高"];
-
- [tableView setTableFooterView:footerLabel];
-
-
- [tableView setRowHeight:50];
-
- [tableView setSeparatorColor:[UIColor redColor]];
-
-
-
-
-
-
-
- [tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
-
- [tableView setBackgroundColor:[UIColor lightGrayColor]];
-
-
- tableView.delegate = self;
- tableView.dataSource = self;
-
- [self.view addSubview:tableView];
-
-
- }
-
- #pragma mark -UITableViewDataSource
-
-
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
-
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- {
- return @"分区开始";
- }
-
-
- -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
- return @"分区结束";
- }
-
-
-
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
-
- return [cityList count];
- }
-
-
- static NSString* cellID = @"cellID";
-
-
-
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
-
- UITableViewCell* tableViewCell = [tableView dequeueReusableCellWithIdentifier:cellID];
-
- if (tableViewCell == nil) {
-
- tableViewCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
- }
-
- tableViewCell.layer.cornerRadius = 12;
- tableViewCell.layer.masksToBounds = YES;
-
-
- NSUInteger rowNo = indexPath.row;
-
- tableViewCell.textLabel.text = [cityList objectAtIndex:rowNo];
-
- tableViewCell.detailTextLabel.text = [NSString stringWithFormat:@"市区"];
-
- tableViewCell.imageView.image = [UIImage imageNamed:@"1.jpg"];
-
- tableViewCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
-
- return tableViewCell;
- }
-
-
- @end
效果图如下:
例子二————分组展现
ViewController.m
-
-
-
-
-
-
-
-
- #import "ViewController.h"
-
- @interface ViewController ()
-
- @end
-
- NSArray* Heroes;
- NSArray* Liqings;
- NSArray* Kazikes;
- NSArray* Rivens;
-
- @implementation ViewController
-
- - (void)viewDidLoad {
- [super viewDidLoad];
-
-
-
- Heroes = @[@"李青",@"卡兹克",@"瑞文"];
-
- Liqings = @[@"李青1",@"李青2"];
-
- Kazikes = @[@"卡兹克"];
-
- Rivens = @[@"瑞文1",@"瑞文2",@"瑞文3"];
-
-
- UITableView* tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame style:UITableViewStyleGrouped];
-
- UILabel* headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
- [headerLabel setText:@"三大英雄"];
-
- [tableView setTableHeaderView:headerLabel];
-
- UILabel* footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
- [footerLabel setText:@"等你来战"];
-
- [tableView setTableFooterView:footerLabel];
-
- [tableView setBackgroundColor:[UIColor cyanColor]];
-
-
- [self.view addSubview:tableView];
-
-
- tableView.dataSource = self;
-
-
- }
-
-
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
- return [Heroes count];
- }
-
-
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
-
- return Heroes[section];
- }
-
-
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
-
- if(section == 0){
- return [Liqings count];
- }else if(section == 1){
- return [Kazikes count];
- }else{
- return [Rivens count];
- }
- }
-
-
- static NSString* cellID = @"cellID";
-
-
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
-
-
- UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];
- if(!cell){
-
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
- }
-
- if(indexPath.section == 0){
- cell.textLabel.text = Liqings[indexPath.row];
-
- cell.imageView.image = [UIImage imageNamed:@"l1.jpg"];
-
- }else if(indexPath.section == 1){
- cell.textLabel.text = Kazikes[indexPath.row];
- cell.imageView.image = [UIImage imageNamed:@"k1.jpg"];
-
- }else{
- cell.textLabel.text = Rivens[indexPath.row];
- cell.imageView.image = [UIImage imageNamed:@"r1.jpg"];
- }
-
- cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
-
- return cell;
- }
-
-
-
- @end
效果图如下:
from: iOS开发之高级视图—— UITableView(一)简单例子
http://blog.csdn.net/panjican/article/details/51493055