IOS開發(24)之儲存格附屬視圖和縮排

來源:互聯網
上載者:User

1 前言
如果我們對IOS SDK提供我們的附屬視圖不滿意的話,我們可以自己自訂附屬視圖,還可以對其進行縮排排版。

2 代碼執行個體
ZYUITableViewController.h:

 

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

#import <UIKit/UIKit.h>

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

@property(nonatomic,strong) UITableView *myTableView;

@end
ZYUITableViewController.m:

 

[plain] view plaincopyprint?@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:UITableViewStylePlain];//設定列表樣式為簡單的樣式 還有一個樣式為UITableViewStyleGrouped為分組模式   UITableViewStylePlain為普通的樣式 
    self.myTableView.delegate = self;//設定代理為自身 
    myTableView.dataSource = self;//設定資料來源為自身 
    self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;//確保TablView能夠正確的調整大小 
    [self.view addSubview:myTableView]; 
     

//設定每行的高度 
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    CGFloat result = 20.0f; 
    if ([tableView isEqual:self.myTableView]) { 
        //        result = 40.0f; 
        result = 80.0f; 
    } 
    return result; 

////允許資料來源告知必須載入到Table View中的表的Section數。 
//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
//    NSInteger result = 0; 
//    if([tableView isEqual:myTableView]){ 
//        result = 3;//一共三個section 
//    } 
//    return result; 
//} 
//預設1個Section 
//設定呈現多少行 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return 10; 

//每行像是的資料 
-(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:@"Cell %ld",(long)indexPath.row]; 
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];//初始化按鈕 
        button.frame = CGRectMake(0.0f,0.0f,150.0f,25.0f);//設定大小 
        [button setTitle:[NSString stringWithFormat:@"Expand %ld",(long)indexPath.row] forState:UIControlStateNormal];//設定按鈕標題和常態樣式 
        [button addTarget:self action:@selector(performExpand:) forControlEvents:UIControlEventTouchUpInside];//添加點擊事件 
        result.accessoryView = button; 
        result.indentationLevel = indexPath.row;//縮排等級 
        result.indentationWidth = 10.0f;//縮排寬度 
         
    } 
    return result; 

 
-(void)performExpand:(UIButton *)param{ 
    NSLog(@"U pressed %@",param.titleLabel.text);//獲得按鈕標題 

 
//點擊某一行時候觸發的事件 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if ([tableView isEqual:myTableView]) { 
        NSLog(@"%@",[NSString stringWithFormat:@"Cell %ld is selected",(long)indexPath.row]); 
    } 

//單擊右側按鈕時候觸發的事件 
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *owenCell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSLog(@"Cell Title = %@",owenCell.textLabel.text);//獲得textLabel的值 

@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:UITableViewStylePlain];//設定列表樣式為簡單的樣式 還有一個樣式為UITableViewStyleGrouped為分組模式   UITableViewStylePlain為普通的樣式
    self.myTableView.delegate = self;//設定代理為自身
    myTableView.dataSource = self;//設定資料來源為自身
    self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;//確保TablView能夠正確的調整大小
    [self.view addSubview:myTableView];
   
}
//設定每行的高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat result = 20.0f;
    if ([tableView isEqual:self.myTableView]) {
        //        result = 40.0f;
        result = 80.0f;
    }
    return result;
}
////允許資料來源告知必須載入到Table View中的表的Section數。
//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//    NSInteger result = 0;
//    if([tableView isEqual:myTableView]){
//        result = 3;//一共三個section
//    }
//    return result;
//}
//預設1個Section
//設定呈現多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
//每行像是的資料
-(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:@"Cell %ld",(long)indexPath.row];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];//初始化按鈕
        button.frame = CGRectMake(0.0f,0.0f,150.0f,25.0f);//設定大小
        [button setTitle:[NSString stringWithFormat:@"Expand %ld",(long)indexPath.row] forState:UIControlStateNormal];//設定按鈕標題和常態樣式
        [button addTarget:self action:@selector(performExpand:) forControlEvents:UIControlEventTouchUpInside];//添加點擊事件
        result.accessoryView = button;
        result.indentationLevel = indexPath.row;//縮排等級
        result.indentationWidth = 10.0f;//縮排寬度
       
    }
    return result;
}

-(void)performExpand:(UIButton *)param{
    NSLog(@"U pressed %@",param.titleLabel.text);//獲得按鈕標題
}

//點擊某一行時候觸發的事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:myTableView]) {
        NSLog(@"%@",[NSString stringWithFormat:@"Cell %ld is selected",(long)indexPath.row]);
    }
}
//單擊右側按鈕時候觸發的事件
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *owenCell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"Cell Title = %@",owenCell.textLabel.text);//獲得textLabel的值
}
運行結果:

 
 


點擊第一行控制台輸出:


2013-04-28 10:56:21.187 UITableViewTest1[877:c07] Cell 0 is selected

點擊第二行按鈕控制台輸出:


2013-04-28 10:56:23.957 UITableViewTest1[877:c07] U pressed Expand 1

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.