UITableView动态存放、重用机制
一、UITableView动态存放
@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被点击时,取消选中状态
二、重用机制
@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 *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];处理重用bug很不友好的方式,不建议使用
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
if (indexPath.row == 1) {
cell.textLabel.text = @"1212";
}
return cell;
UITableView动态存放、重用机制的更多相关文章
- iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法
"UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...
- 解决UITableView中Cell重用机制导致内容出错的方法总结
UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...
- ios UITableView中Cell重用机制导致内容重复解决方法
UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...
- iOS学习笔记(4)— UITableView的重用机制
UITableView中的cell是动态的,在使用过程中,系统会根据屏幕的高度(480)和每个cell的高度计算屏幕中需要显示的cell的个数.比如,cell高度为90.那么480 / 90 = 5 ...
- iOS学习笔记(4) — UITableView的 重用机制
iOS学习笔记(4) — UITableView的 重用机制 UITableView中的cell是动态的,在使用过程中,系统会根据屏幕的高度(480)和每个cell的高度计算屏幕中需要显示的cell的 ...
- iOS基础篇(十三)——UITableView(一)重用机制
UITableView是app开发中常用到的控件,功能很强大,常用于数据的显示.在学习UITableView使用之前,我们先简单了解一下: 1.UITableView的重用机制 UITableView ...
- UI之UItableView重用机制的性能问题
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 操蛋的UITableView重用机制
1,背景 你可能会遇见一下情况: 使用UITableView加载数据,比如你的每一个cell上面有一个UITextField,当你在第一个cell的UITextField中写下了内容,开始下滑UITa ...
- IOS开发—UITableView重用机制的了解
引言 对于一个UITableView而言,可能需要显示成百上千个Cell,如果每个cell都单独创建的话,会消耗很大的内存.为了避免这种情况,重用机制就诞生了. 假设某个UITableView有100 ...
随机推荐
- linux文件上传
租了个服务器,要把自己打的war包打上去发布,服务器是linux的(本机是mac).以前都是用wget命令,把需要上传的东西先放到github上面再在服务器上用wget命令下载.最近不知道什么原因,w ...
- JavaScript实现类的private、protected、public、static以及继承
JavaScript中的类 JavaScript实际上是一种弱类型语言,与C++和Java等语言不同.因此,在JavaScript中,没有强调类(class)这一概念,但实际运用中,类还是很重要的,比 ...
- NKUI框架使用
使用条件: css添加引用: <link rel="stylesheet" href="$rootPath/themes/default/css/tools/nku ...
- Ubuntu14.04安装MySQL-python异常: mysql_config: not found,Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-MJWMPd/MySQL-python/
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABUoAAAE8CAIAAACZ6RwMAAAgAElEQVR4nOydaVxTx9fHn9dhKWqltv
- Linux(centos)系统各个目录的作用详解
Linux(centos)系统各个目录的作用详解 文件系统的类型 LINUX有四种基本文件系统类型:普通文件.目录文件.连接文件和特殊文件,可用file命令来识别. 普通文件:如文本文件.C语言元代码 ...
- UML类图画法及其之间的几种关系
最近做重构项目,需要画一下类图,发现类图的画法及其之间的几种关系已经淡忘了很多,所以整理总结一下,有问题的地方大家可以一起讨论下. 文章目录如下: 类图画法 类之间的几种关系:泛化(Generaliz ...
- 手机抓包xcode自带命令行工具配合wireshark实现
三.最佳方式:rvictl命令 优点:简单,而且可以抓所有网络接口的数据: 缺点:似乎没有,要求手机iOS5以上不算要求吧?如果说缺点,就是这个命令是Xcode的Command Line Tools ...
- MySQL高可用方案选型参考
http://imysql.com/2015/09/14/solutions-of-mysql-ha.shtml#rd?sukey=b0cb5c5b9e501303123bd48e86555c35e0 ...
- Volley框架之网络请求和图片加载
Volley是 Google 推出的 Android 异步网络请求框架和图片加载框架. Volley的特性 (1).封装了的异步的请求API.Volley 中大多是基于接口的设计,可配置性强.(2). ...
- Linux编程下EAGAIN和EINTR宏的含义及处理
Linux中的EAGAIN含义 在Linux环境下开发经常会碰到很多错误(设置errno),其中EAGAIN是其中比较常见的一个错误(比如用在非阻塞操作中). linux下使用write\send ...