一、UITableView动态存放

#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{
    UITableView *tableview;
    NSMutableArray *_arr;//盛放要显示的东西
}
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //tableview.rowHeight = 80;//设置行高
    tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//分割线样式
    //tableview.separatorColor =[UIColor yellowColor];//分割线颜色
    UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"23.jpg"]];
    tableview.backgroundView = imageview;
    tableview.delegate = self;
    tableview.dataSource = self;//设置数据源
    tableview.backgroundView = imageview;//设置背景图
    [self.view addSubview:tableview];
   
    _arr = [NSMutableArray arrayWithObjects:@"222",@"666",@"555",@"999", nil];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor groupTableViewBackgroundColor];
    button.frame = CGRectMake(300, 20, 60, 60);
    [button setTitle:@"add" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(addcell) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
//点击button,tableview的Cell个数+1
-(void)addcell{
   
//    NSArray *arr1 = [NSArray arrayWithObjects:@"王",@"冉", nil];
//    [_arr addObjectsFromArray:arr1];
    [_arr addObject:@"王"];
    [tableview reloadData];//当tableView的数据源发生改变时,调用该方法,会更新tableView的显示,
}
//是否允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
//提交修改动作:  再执行删除之前,需要先移除数组中对应的元素,
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    [_arr removeObjectAtIndex:indexPath.row];
    [tableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];//删除行
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableview dequeueReusableHeaderFooterViewWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;//设置选中时的背景颜色
   
    UIView *view = [[UIView alloc]initWithFrame:cell.frame];
    view.backgroundColor = [UIColor greenColor];
    cell.selectedBackgroundView = view;
    cell.textLabel.text = _arr[indexPath.row];
    cell.textLabel.highlightedTextColor = [UIColor brownColor];//选中状态下,字体颜色
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//自定义右侧视图
    return cell;
}
//当cell的accessoryStyle中包含信息按钮(叹号)时,点击按钮触发的方法
-(void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
}
//取消选中
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    //NSLog(@"取消选中");
    //NSLog(@"%zi",indexPath.row);
}
//cell被选中
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableview deselectRowAtIndexPath:indexPath animated:YES];//当cell被点击时,取消选中状态
}

二、重用机制

重用机制:当我们使用tableView时,系统只会创建屏幕中显示的cell个数+1,当cell滑出可视范围时,会将此Cell放入重用池,当有新的cell滑进可视范围时,会先到重用池找有没有可以使用的cell(根据标识),如果能找到,则拿出来直接用,否则再创建新的cell
 
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource>
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];

UITableView *table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    table.dataSource = self;
    [self.view addSubview:table];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 40;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 
   // NSString *identifier = [NSString stringWithFormat:@"cell%zi%zi",indexPath.section,indexPath.row];//给每行设置不同的标识,以解决重用机制的产生的bug
 
    static NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];处理重用bug很不友好的方式,不建议使用
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
 
    cell.textLabel.text = nil;//先清理,在使用
   
    if (indexPath.row == 1) {
        cell.textLabel.text = @"1212";
    }
    return cell;
}

UITableView动态存放、重用机制的更多相关文章

  1. iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法

    "UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...

  2. 解决UITableView中Cell重用机制导致内容出错的方法总结

    UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...

  3. ios UITableView中Cell重用机制导致内容重复解决方法

    UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...

  4. iOS学习笔记(4)— UITableView的重用机制

    UITableView中的cell是动态的,在使用过程中,系统会根据屏幕的高度(480)和每个cell的高度计算屏幕中需要显示的cell的个数.比如,cell高度为90.那么480 / 90 = 5  ...

  5. iOS学习笔记(4) — UITableView的 重用机制

    iOS学习笔记(4) — UITableView的 重用机制 UITableView中的cell是动态的,在使用过程中,系统会根据屏幕的高度(480)和每个cell的高度计算屏幕中需要显示的cell的 ...

  6. iOS基础篇(十三)——UITableView(一)重用机制

    UITableView是app开发中常用到的控件,功能很强大,常用于数据的显示.在学习UITableView使用之前,我们先简单了解一下: 1.UITableView的重用机制 UITableView ...

  7. UI之UItableView重用机制的性能问题

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  8. 操蛋的UITableView重用机制

    1,背景 你可能会遇见一下情况: 使用UITableView加载数据,比如你的每一个cell上面有一个UITextField,当你在第一个cell的UITextField中写下了内容,开始下滑UITa ...

  9. IOS开发—UITableView重用机制的了解

    引言 对于一个UITableView而言,可能需要显示成百上千个Cell,如果每个cell都单独创建的话,会消耗很大的内存.为了避免这种情况,重用机制就诞生了. 假设某个UITableView有100 ...

随机推荐

  1. 【JavaScript】前端插件

    树形结构: http://www.jeasyui.com/documentation/index.php 网上有对这个插件的说明,总的来说这个插件将selected和checked作为两种状态: 1. ...

  2. js鼠标点击版tab切换

    代码很简单,主要是布局需要用心研究下,使用时需要把css内注释去除 <!DOCTYPE html> <head> <meta http-equiv="Conte ...

  3. JAVA编程规则【转自java编程思想】

    本附录包含了大量有用的建议,帮助大家进行低级程序设计,并提供了代码编写的一般性指导: (1) 类名首字母应该大写.字段.方法以及对象(句柄)的首字母应小写.对于所有标识符,其中包含的所有单词都应紧靠在 ...

  4. 编译caffe报错:_ZN5boost16exception_detail10bad_alloc_D2Ev

    具体报错信息很长的. text._ZN5boost16exception_detail10bad_alloc_D2Ev[_ZN5boost16exception_detail10bad_alloc_D ...

  5. scala.collection.immutable.HashSet$.empty()Lscala/collection/immutable/HashSet

    最近重新搭了spark环境.在Master上使用了IDEA来写代码.确实很方便.我用的是hadoop2.6.spark1.5.1forhadoop2.6. scala之前用的是2.11.0老是报这个错 ...

  6. wamp密码设置

    WAMP安装好后,mysql密码是为空的,那么要如何修改呢?其实很简单,通过几条指令就行了,下面我就一步步来操作. 首先,通过WAMP打开mysql控制台. 提示输入密码,因为现在是空,所以直接按回车 ...

  7. python获取DBLP数据集

    #!/usr/bin/python # -*- coding: UTF-8 -*- import xml.sax import io, sys paper_tags = ('article', 'in ...

  8. androidannotations 简单配置

    1.build.gradle 需要添加的内容 标注的颜色是新建项目之后,build.gradle文件需要添加的内容. buildscript { repositories { jcenter() } ...

  9. 使用max() 函数

    /********************************* 代码功能:使用串口输出a和b的最大值 使用函数:max(a,b); 创作时间:2016*11*04 作者邮箱:jikexianfe ...

  10. html 表格的制作

    表格的制作 表格<table></table> <table  width=""  height=""  align=" ...