iOS中表格(UITableView)巢狀表格格的簡單實現

來源:互聯網
上載者:User

iOS中表格(UITableView)巢狀表格格的簡單實現

iOS中表格(UITableView)巢狀表格格的簡單實現

首先說一下思路:我們在一個控制器裡面定義2個tableview,一個作為被嵌套的rootTable,一個作為嵌套的表格tableView1,那我們要實現UITableViewDelegate,UITableViewDataSource,的代理的時候,該怎麼區分呢?其實很簡單,有兩種方法,一個是給定義的2個tableview設定tag值,另一個是直接寫出來tableView == rootTable時實現他得代理,否則就實現tableView1的代理方法。

測試環境 Xcode6.1
Demo的:


下面是實現的代碼:

ViewController.h#import @interface ViewController : UIViewController{    UITableView * rootTable;    UITableView * tableView1;    NSMutableArray * ChildArr;    //NSMutableArray * ChildArr1;    //NSMutableArray * ChildArr2;}@endViewController.m#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    [self initView];    ChildArr = [[NSMutableArray alloc]initWithObjects:@"蘋果",@"栗子",@"香蕉",@"菠蘿",@"桃子", @"荔枝",nil];    self.navigationItem.title = @"TwoTableView";    }-(void)initView{    rootTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];    rootTable.delegate = self;    rootTable.dataSource = self;    [self.view addSubview:rootTable];    }-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    if (tableView == rootTable)    {        if (indexPath.row == 0)        {            return [ChildArr count]*44;        }else        {           return 70;        }    }else    {        return 44;    }}-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    if (tableView == rootTable)    {        return 5;    }else    {        return 1;    }}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    if (tableView == rootTable)    {        return 4;    }else    {        return [ChildArr count];    }}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell * cell = [[UITableViewCell alloc]init];    if (tableView == rootTable)    {        if (indexPath.row == 0)        {             tableView1 = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, [ChildArr count]*44)];             tableView1.delegate = self;             tableView1.dataSource = self;             tableView1.scrollEnabled = NO;             [cell.contentView addSubview:tableView1];        }else        {            cell.textLabel.text = @"rootTableView";        }        return cell;    }else    {        cell.textLabel.text = [ChildArr objectAtIndex:indexPath.row];        cell.backgroundColor = [UIColor yellowColor];        return cell;    }}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    if (tableView == rootTable)    {        NSLog(@"roottableView");    }else    {        NSLog(@"蘋果");    }}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end


聯繫我們

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