【代碼筆記】iOS-TableViewOfTwoSecton,代碼筆記

來源:互聯網
上載者:User

【代碼筆記】iOS-TableViewOfTwoSecton,代碼筆記

一,。

二,工程圖。

三,代碼。

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

相關文章

聯繫我們

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