iOS中表格(UITableView)嵌套表格的简单实现
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了iOS中表格(UITableView)嵌套表格的简单实现,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2798字,纯文字阅读大概需要4分钟。
内容图文
iOS中表格(UITableView)嵌套表格的简单实现
首先说一下思路:我们在一个控制器里面定义2个tableview,一个作为被嵌套的rootTable,一个作为嵌套的表格tableView1,那我们要实现UITableViewDelegate,UITableViewDataSource,的代理的时候,该怎么区分呢?其实很简单,有两种方法,一个是给定义的2个tableview设置tag值,另一个是直接写出来tableView
== rootTable时实现他得代理,否则就实现tableView1的代理方法。
测试环境 Xcode6.1
Demo的下载地址:
下面是实现的代码:
ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> { UITableView * rootTable; UITableView * tableView1; NSMutableArray * ChildArr; //NSMutableArray * ChildArr1; //NSMutableArray * ChildArr2; } @end ViewController.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
原文:http://blog.csdn.net/jingjingxujiayou/article/details/41926061
内容总结
以上是互联网集市为您收集整理的iOS中表格(UITableView)嵌套表格的简单实现全部内容,希望文章能够帮你解决iOS中表格(UITableView)嵌套表格的简单实现所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。