iOS编程-------UITableView表视图 / UITableViewCell的重用机制

//
//  AppDelegate.h
//  UI09_UITableView表视图_重用机制
//
//  Created by  on 15/9/11.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end




//
//  AppDelegate.m
//  UI09_UITableView表视图_重用机制
//
//  Created by l on 15/9/11.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate
- (void)dealloc{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

   //rootVC
    RootViewController *rootVC = [[RootViewController alloc] init];
    self.window.rootViewController = rootVC;
    [rootVC release];



    return YES;
}

@end





//





//
//  RootViewController.h
//  UI09_UITableView表视图_重用机制
//
//  Created by l on 15/9/11.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end






//
//  RootViewController.m
//  UI09_UITableView表视图_重用机制
//
//  Created by l on 15/9/11.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation RootViewController



#pragma mark-- UITableViewDataSource
//返回分区数,默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 10;// 10个分区
}

//设置分区中的行数,(必须实现)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;//每个分区有10行

}

//设置分区头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    return [NSString stringWithFormat:@"第%ld区头", section];
}

//设置分区尾标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

    return [NSString stringWithFormat:@"第%ld区尾", section];

}

//右侧竖排索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    return @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10"];
}

//加载单元格(必须实现)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //单元格的重用机制
    //(1)每次创建单元格之前都去重用队列里面寻找,是否有可重用的单元格
    //(2)如果有,则是使用,如果没有可以重用的单元格,创建新的单元格并标上重用标识符

    //1.创建重用标识符
   static NSString *identifier = @"cell";
    //2.从tabView的重用队列中,取可重用的tableViewCell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //3.判断 如果没有则创建新的单元格
    if (cell == nil) {
        //注意加上重用标识符和autorelease
        cell = [[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier] autorelease];
    }
    static int i = 1;
    NSLog(@"%d", i++);

    //4.设置
     cell.textLabel.text = [NSString stringWithFormat:@"第%ld分区,第%ld行",indexPath.section, indexPath.row];
//
    //5.返回


//    //1.创建单元格
//    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:nil];
    //2.设置cell (给单元格设置内容)
    //设置单元格的图片
    UIImage *image = [UIImage imageNamed:@"w.jpg"];
    cell.imageView.image = image;
//
//    //NSIndexPath 下表路径
//    //我们主要使用的是NSIndexPath(tableView)分区
//    //selection 分区号
//    //row 行号
//    
//    //获取当前分区
//    NSInteger section = indexPath.section;
//    //获取分区中行号
//    NSInteger row = indexPath.row;
//
//    //textLabel标题
//    cell.textLabel.text = @"单元格的标题";
//     cell.textLabel.text = [NSString stringWithFormat:@"第%ld分区,第%ld行", section, row];
//
//    //详情标题
//    cell.detailTextLabel.text = @"详情标题";
//    //3.返回单元格
    return cell;

}

#pragma mark--重写loadView

- (void)loadView{

    //1.创建表视图 UITableView
    UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
    //2.设置属性
    //常用属性 分割线颜色,分割线样式,行高等
//    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//分割线样式,默认为singleLine
    tableView.separatorColor = [UIColor redColor];//分割线颜色

    tableView.rowHeight = 200;//行高

    //表头和表尾宽度等屏,我们只可以控制高度
    //高度的控制通过设置tableHeaderView.frame
    UIView *headerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, 0, 50))];
    headerView.backgroundColor = [UIColor blueColor];
    tableView.tableHeaderView = headerView;
    //表尾
    UIView *footerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, 0, 50))];
    footerView.backgroundColor = [UIColor redColor];
    tableView.tableFooterView = footerView;

    /**
     重点: 表视图的代理,表视图的数据源
     代理控制外观
     数据源控制数据

     */

    //设置表视图的代理
    tableView.delegate = self;
    //设置数据源
    tableView.dataSource = self;

    //3.添加
    self.view = tableView;

    //4.释放
    [tableView release];



}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值