UI4_UITableViewEdit

//

//  AppDelegate.m

//  UI4_UITableViewEdit

//

//  Created by zhangxueming on 15/7/13.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import "AppDelegate.h"

#import "ViewController.h"





@interface AppDelegate ()



@end



@implementation AppDelegate





- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    ViewController *root = [[ViewController alloc] init];

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];

    self.window.rootViewController = nav;

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    

    return YES;

}





//

//  ViewController.h

//  UI4_UITableViewEdit

//

//  Created by zhangxueming on 15/7/13.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import <UIKit/UIKit.h>



@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>





@end

 

//

//  ViewController.m

//  UI4_UITableViewEdit

//

//  Created by zhangxueming on 15/7/13.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import "ViewController.h"



@interface ViewController ()

{

    UITableView *_tableView;

    NSMutableArray *_dataList;

}



@end



@implementation ViewController



- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];

    [self.view addSubview:_tableView];

    

    //创建数据源

    _dataList = [NSMutableArray array];

    

    NSMutableArray *boys = [NSMutableArray array];

    for (int i=0; i<10; i++) {

        NSString *name = [NSString stringWithFormat:@"boy%d", i];

        [boys addObject:name];

    }

    [_dataList addObject:boys];

    

    NSMutableArray *grils = [NSMutableArray array];

    for (int i=0; i<15; i++) {

        NSString *name = [NSString stringWithFormat:@"gril%d", i];

        [grils addObject:name];

    }

    [_dataList addObject:grils];

    

    _tableView.delegate = self;

    _tableView.dataSource = self;

    

    //编辑状态

    self.navigationItem.leftBarButtonItem = self.editButtonItem;

}



//设置表示图到编辑状态



- (void)setEditing:(BOOL)editing animated:(BOOL)animated

{

    [super setEditing:editing animated:animated];

    //让tableView处在编辑状态

    [_tableView setEditing:editing animated:YES];

}



#pragma mark ---UITableViewDataSource---

//返回分区的行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return [[_dataList objectAtIndex:section] count];

}



//返回分区的个数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return _dataList.count;

}



//创建UITableViewCell



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *reuseIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];

    }

    NSArray *array = [_dataList objectAtIndex:indexPath.section];

    cell.textLabel.text = [array objectAtIndex:indexPath.row];

    cell.detailTextLabel.text = [NSString stringWithFormat:@"age:%d",arc4random()%40+10];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    return cell;

}



- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    if (section==0) {

        return @"男孩";

    }

    return @"女孩";

}





- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end

 

你可能感兴趣的:(UITableView)