IOS開發(26)之UITableView的頁首和頁尾

來源:互聯網
上載者:User

1 前言
UITableView中的每個Section中都可以設定頁首和頁尾,來滿足需求。使用者都可以自己設定。

2 代碼執行個體
ZYViewHeaderFooterController.h:

 

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

#import <UIKit/UIKit.h>

@interface ZYViewHeaderFooterController : UIViewController<UITableViewDelegate,UITableViewDataSource>//添加代理

@property(nonatomic,strong) UITableView *myTableView;

@end
ZYViewHeaderFooterController.m:

 

[plain]
@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; 

@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;
}
運行結果:

 
 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.