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

cell的重用机制

时间:2015-12-02 22:20:43      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

重用机制

简单的说 意思 一行一行 的cell 都是在复用的, 滑动 tableview 的时候,刚离开视图的 cell 会被放到复用池 中,等下一个 cell需要 显示时,会先看复用池中有没有 cell 如果有的时候 ,就从复用池中拿出来cell ,没有的话就重新创建cell.

废话不多说直接上代码:

//

//  ViewController.m

//  tableView

//

//  Created by WBapple on 15/12/1.

//  Copyright © 2015年 apple . All rights reserved.

//

 

#import "ViewController.h"

 

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    _tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];

    _tableView.backgroundColor = [UIColor redColor];

    _tableView.rowHeight = 40;

    _tableView.delegate = self;

    _tableView.dataSource = self;

    [self.view addSubview:_tableView];

    

 

}

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

    return 4;

 

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 4;

}

 

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

    static NSString *cellId = @"cell";

    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];

    if(cell == nil)

    {

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

    }

    cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.section];

    //设置每个cell的内容

    if (indexPath.section == 0) {

        if (indexPath.row ==0) {

            cell.textLabel.text = @"设为默认地址:";

            

            //创建一个switch并添加

            UISwitch *abcd = [[UISwitch alloc]initWithFrame:CGRectMake(265, 6, 0, 0)];

            //改变switch的位置

            //                abcd = [[UISwitch alloc]initWithFrame:CGRectMakeF(265, 6, 0, 0)];

            //改变switch的状态

            abcd.on = YES;

            //添加

            [cell.contentView addSubview:abcd];

            

            

            

            

            

        }

        if (indexPath.row == 1) {

            cell.textLabel.text = @"收件姓名:";

            //设置lable颜色

            cell.textLabel.textColor = [UIColor redColor];

            

        }

        if (indexPath.row == 2) {

            cell.textLabel.text = @"手机号码:";

        }

    }else {

        if (indexPath.row ==0) {

            cell.textLabel.text = @"设为默认地址:";

        }

        if (indexPath.row == 1) {

            cell.textLabel.text = @"收件姓名:";

        }

        if (indexPath.row == 2) {

            cell.textLabel.text = @"手机号码:";

        }

    }

    

    

    return cell;

    

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

 

cell的重用机制

标签:

原文地址:http://www.cnblogs.com/wangbinios/p/5014206.html

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