【代码笔记】iOS-TableViewOfTwoSecton
2017-06-14 09:34
302 查看
一,效果图。
二,工程图。
三,代码。
RootViewController.h
RootViewController.m
二,工程图。
三,代码。
RootViewController.h
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> { NSArray * dataArray; NSArray * aboutArray; } @end
RootViewController.m
#import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (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. self.title=@"tableViewOfTwoSection"; //初始化背景图 [self initBackGroundView]; //初始化数据 [self initData]; } #pragma -mark -funcitons -(void)initBackGroundView { UITableView * tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 376) style:UITableViewStyleGrouped]; tableview.delegate = self; tableview.dataSource = self; [self.view addSubview:tableview]; } -(void)initData { dataArray = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"脑筋急转弯", @"title", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"儿童饮食", @"title", nil], [NSDictionary dictionaryWithObjectsAndKeys:@"儿童健康", @"title", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"宝宝资讯", @"title", nil],nil]; aboutArray = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"关于", @"title", @"aboutViewController", @"class", nil], nil]; } #pragma -mark -UITableViewDelegate -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(section==0) { return dataArray.count; } else if(section==1) { return aboutArray.count; } return 0; } -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID"]; if(cell==nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"ID"]; } if(indexPath.section==0){ cell.textLabel.text =[[dataArray objectAtIndex:indexPath.row]objectForKey:@"title"]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; }else if(indexPath.section==1) { cell.textLabel.text = [[aboutArray objectAtIndex:indexPath.row]objectForKey:@"title"]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.section==0) { if(indexPath.row==0) { NSLog(@"脑筋急转弯"); }else if (indexPath.row==1){ NSLog(@"儿童饮食"); }else if (indexPath.row==2){ NSLog(@"儿童健康"); }else if (indexPath.row==3){ [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://baby.163.com"]]; } }else if (indexPath.section==1) { if(indexPath.row==0) { NSLog(@"关于"); } } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- 【代码笔记】iOS-TableViewOfTwoSecton
- 【代码笔记】iOS-UIScrollerView里有两个tableView
- 【代码笔记】iOS-一个tableView,两个section
- 【代码笔记】iOS-一个tableView,两个section
- iOS开发 自定义tableView样式(使用代码/使用Interface Builder)、分组显示、给TableView增加索引、给TableView增加SearchBar
- iOS开发笔记--TableView 详细解释
- IOS学习笔记 -- scrollView和tableView整理
- IOS 学习笔记 —— tableView 使用详解(二)
- iOS下用一行代码实现tableview(collectionview)的parallax效果
- IOS 学习笔记(9)tableView基础
- iOS开发笔记--异步加载图片在TableView中的应用
- iOS开发笔记--tableview重用问题
- IOS开发之TableView、多个TableViewCell、自定义Cell、Cell上画画(故事板+代码方式)
- iOS开发笔记--多个Section的TableView
- IOSUI课堂笔记用TableView实现省市区
- ((ios开发学习笔记 十一))自定义TableViewCell 的方式实现自定义TableView(带源码)
- IOS开发学习笔记(四)——使用TableView
- iOS学习笔记---数据库SQLite3的基本操作并讲内容显示在tableView中
- IOS 学习笔记(9)tableView基础
- 【iOS7开发笔记】tableview之通过代码自定义cell(cell的高度不一致)