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

UI3_UITableView

时间:2015-07-13 20:22:10      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:

//
//  AppDelegate.m
//  UI3_UITableView
//
//  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
//  UI3_UITableView
//
//  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
//  UI3_UITableView
//
//  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.
    //UITableView继承UIScrollView
    //UITableViewStyleGrouped 分组
    //UITableViewStylePlain 无风格
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //tableView 模式横向不能滚动,只能竖向滚动,contentSize自动计算
    _tableView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:_tableView];
    
    //创建数据源
    _dataList = [NSMutableArray array];
    for (int i=‘A‘; i<=‘Z‘; i++) {
        NSMutableArray *nameArray = [NSMutableArray array];
        NSInteger count = arc4random()%10+1;
        for (int j=0; j<count; j++) {
            NSString *name = [NSString stringWithFormat:@"%c%d-name", (char)i, j];
            [nameArray addObject:name];
        }
        [_dataList addObject:nameArray];
    }
    self.automaticallyAdjustsScrollViewInsets = YES;
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
}

#pragma mark ---UITableViewDataSource---

//返回每个分区中的显示的行数
//有多少个分区, 该方法被调用多少次
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[_dataList objectAtIndex:section] count];
}

//重用机制

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //设定cell的重用标识符
    static NSString *cellID = @"cell";
    //先从表单元格重用队列中取出重用标识符的对象,如果为空,则创建表单元格对象
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    
    NSArray *nameArray = [_dataList objectAtIndex:indexPath.section];
    NSString *name = [nameArray objectAtIndex:indexPath.row];
    cell.textLabel.text  = name;
    cell.detailTextLabel.text = @"副标题test";
    //设置选中状态的风格
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    //设置右侧指示风格
    //详情风格
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    //设置左侧图片
    cell.imageView.image = [UIImage imageNamed:@"friend_weixin@2x"];
    return cell;
}

//返回分区的个数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_dataList count];
}

//设置分区的头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"第%c行",(char)(section+‘A‘)];
}

//返回表单元格的高度,默认是44
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

//选中表单元格的时候调用该方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"该表单元格被选中 section = %li row = %li", indexPath.section, indexPath.row);
    //设置表单元格为非选中状态,
    //[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//表单元格被取消时调用
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"该表单元格被取消 section = %li row = %li", indexPath.section, indexPath.row);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

UI3_UITableView

标签:

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

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