IOS开发(26)之UITableView的页眉和页脚

来源:互联网 发布:软件配置文件用途 编辑:程序博客网 时间:2024/06/05 16:36

1 前言

UITableView中的每个Section中都可以设置页眉和页脚,来满足需求。用户都可以自己设置。

2 代码实例

ZYViewHeaderFooterController.h:

#import <UIKit/UIKit.h>@interface ZYViewHeaderFooterController : UIViewController<UITableViewDelegate,UITableViewDataSource>//添加代理@property(nonatomic,strong) UITableView *myTableView;@end

ZYViewHeaderFooterController.m:

@synthesize myTableView;- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.    self.view.backgroundColor = [UIColor whiteColor];    myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];//设置列表样式为简单的样式 还有一个样式为UITableViewStyleGrouped为分组模式   UITableViewStylePlain为普通的样式    self.myTableView.delegate = self;//设置代理为自身    myTableView.dataSource = self;//设置数据源为自身    self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;//确保TablView能够正确的调整大小    [self.view addSubview:myTableView];}//设置每个Section呈现多少行-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return 3;}//每行像是的数据-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *result = nil;    if ([tableView isEqual:myTableView]) {        static NSString *tableViewCellIdentifier = @"MyCells";//设置Cell标识        result = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];//通过标示符返回一个可重用的表视图单元格对象        if (result == nil) {            result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCellIdentifier];//初始化一个表格单元格样式和重用的标识符,并将它返回给调用者。        }        //indexPath.section 表示section的索引 indexPath.row表示行数的索引        result.textLabel.text = [NSString stringWithFormat:@"Section %ld,Cell %ld",(long)indexPath.section,(long)indexPath.row];    }    return result;}//设置Section的Header-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    NSString *result = nil;    if ([tableView isEqual:myTableView]&§ion==0) {        result = @"Section 0 Header";    }    return result;}//设置Section的Footer-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{    NSString *result = nil;    if ([tableView isEqual:myTableView]&§ion==0) {        result = @"Section 0 Header";    }    return result;}

运行结果:


3 结语

以上就是所有内容,希望对大家有所帮助。

Demo实例:http://download.csdn.net/detail/u010013695/5321657

原创粉丝点击