码迷,mamicode.com
首页 > 其他好文 > 详细

UI4_UITableViewEdit

时间:2015-07-13 20:16:52      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:

//
//  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

 

UI4_UITableViewEdit

标签:

原文地址:http://www.cnblogs.com/0515offer/p/4643558.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!