• 效果展示
  • 过程分析
  • 代码实现
  • cell缓存池优化

一、效果展示

二、过程分析

  1. 首先通过三步创建数据,展示数据
  2. 监听选中某一个cell时调用的方法
  3. 在cell中创建一个对话框
  4. 修改对话框中的值,并且重新刷新选中的那个cell

三、代码实现

contact.h

#import <Foundation/Foundation.h>

@interface Contact : NSObject

@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *desc; + (id)contactWithIcon:(NSString*)icon title:(NSString*)title desc:(NSString*)desc; @end

contact.m

#import "Contact.h"

@implementation Contact

+ (id)contactWithIcon:(NSString *)icon title:(NSString *)title desc:(NSString *)desc
{
Contact *contact = [[Contact alloc] init];
contact.icon = icon;
contact.title = title;
contact.desc = desc; return contact;
} @end

ViewController.m

//
// ViewController.m
// 01-UITableView的单组数据
//
// Created by apple on 14-4-3.
// Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
// #import "ViewController.h"
#import "Contact.h" @interface ViewController () <UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>
{
NSMutableArray *contacts;
}
@end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad]; Contact *contact1 = [Contact contactWithIcon:@"010.png" title:@"盖聂" desc:@"剑圣"];
Contact *contact2 = [Contact contactWithIcon:@"011.png" title:@"白凤" desc:@"轻功"];
Contact *contact3 = [Contact contactWithIcon:@"012.png" title:@"胜七" desc:@"巨阙"]; contacts = [NSMutableArray array];
[contacts addObjectsFromArray:@[contact1, contact2, contact3]]; for (int i = ; i < ; i++) {
NSString *icon = [NSString stringWithFormat:@"01%d.png", i%];
NSString *title = [NSString stringWithFormat:@"人物%d", i + ];
Contact *contact = [Contact contactWithIcon:icon title:title desc:title]; [contacts addObject:contact];
}
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return contacts.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
Contact *contact = contacts[indexPath.row]; cell.textLabel.text = contact.title;
cell.detailTextLabel.text = contact.desc;
cell.imageView.image = [UIImage imageNamed:contact.icon];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ;
}
//选中某一个cell的时候调用该方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Contact *c = contacts[indexPath.row]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:c.title message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert textFieldAtIndex:].text = c.desc;
alert.tag = indexPath.row; [alert show];
}
//监听点击了UIAlertView里面的按钮
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == ) return; Contact *c = contacts[alertView.tag]; c.desc = [alertView textFieldAtIndex:].text; //更新全部数据
//[_tableView reloadData];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:alertView.tag inSection:];
NSArray *indexs = @[indexPath];
[_tableView reloadRowsAtIndexPaths:indexs withRowAnimation:UITableViewRowAnimationBottom];
} @end

四、cell缓存池优化

  1. 用到时创建(苹果已经帮我们完成)
  2. cell缓存池中取出可循环的cell

So easy

static NSString *cellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell];
} NSLog(@"%p--%d", cell, indexPath.row);

通过打印内存地址可以看出,cell被重复利用了

应用程序之UITableView的Plain用法和cell缓存池优化的更多相关文章

  1. UITableView的常用属性和cell的内存优化

    UITableView的常用属性: 分割线颜色设置: 1> 设置separatorStyle: 分割线的颜色 方法:tableView.separatorStyle = UITableViewC ...

  2. UITableView系列(1)---Apple缓存池机制

    一.概述 关于UITableView的基本使用, 其实十分简单.但是做App最重要的之一就是细致,技术方面要做到细致, 必须深入了解底层, 才能做出优化让程序跑得更快.那么这一系列文章从我实际项目中获 ...

  3. iOS - UITableView中有两种重用Cell的方法

    UITableView中有两种重用Cell的方法: - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; - (id)dequ ...

  4. 程序员收藏必看系列:深度解析MySQL优化(二)

    程序员收藏必看系列:深度解析MySQL优化(一) 性能优化建议 下面会从3个不同方面给出一些优化建议.但请等等,还有一句忠告要先送给你:不要听信你看到的关于优化的“绝对真理”,包括本文所讨论的内容,而 ...

  5. 微信小程序开发:学习笔记[9]——本地数据缓存

    微信小程序开发:学习笔记[9]——本地数据缓存 快速开始 说明 本地数据缓存是小程序存储在当前设备上硬盘上的数据,本地数据缓存有非常多的用途,我们可以利用本地数据缓存来存储用户在小程序上产生的操作,在 ...

  6. 程序设计模式浅析(plain framework商业版设计模式)

    程序设计其实对程序开发者来说十分重要,但是在工作中往往我们却忽略了这一块,因为我们所用的都是现有的模式.一个设计模式的好坏,往往能够体现出程序的专业性,还有整个项目的可持续性.这就是为什么有些公司,在 ...

  7. iOS 7 UITableview 在Plain模式下 设置背景颜色无效

    在iOS6的时候,设置Plain模式下table的颜色 通过[self.table setBackgroundColor:[UIColor red]]; 就可以看到一个满身通红的tableView 但 ...

  8. 应用程序之UITableView的编辑模式

    cell分层结构 效果展示 代码实现 一.cell的分层结构 二.效果展示 三.代码实现 // // ViewController.m // 01-TableView的删除实现 // // Creat ...

  9. UITableView的plain样式下,取消区头停滞效果

    核心代码 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat sectionHeaderHeight = sectionH ...

随机推荐

  1. HDU6438 Buy and Resell 解题报告(一个有趣的贪心问题的严格证明)

    写在前面 此题是一个很容易想到的贪心题目,但是正确性的证明是非常复杂的.然而,目前网上所有题解并未给出本题贪心算法的任何正确性证明,全部仅停留在描述出一个贪心算法.本着对算法与计算机科学的热爱(逃), ...

  2. CCC2019游记

    好吧其实是清华游记,$CCC2019$ 在中国只有北京和天津举办,要选去加拿大的人很少,估计是最近两国关系有点紧张的缘故吧 但实际上是某些已经被清华钦点的人去预览一下他们未来的栖息所 $13:30$ ...

  3. python获取目录下文件夹名称

    path = '/opt' dirs = os.listdir(path) for dir in dirs: print dir

  4. 2017-2018-2 20179204《网络攻防实践》第十三周学习总结 python实现国密算法

    国密商用算法是指国密SM系列算法,包括基于椭圆曲线的非对称公钥密码SM2算法.密码杂凑SM3算法.分组密码SM4算法,还有只以IP核形式提供的非公开算法流程的对称密码SM1算法等. 第1节 SM2非对 ...

  5. POJ1927 Area in Triangle

      Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1458   Accepted: 759 Description Give ...

  6. 透明层提示框代替windows警告窗口方法!

    原文发布时间为:2009-04-05 -- 来源于本人的百度文章 [由搬家工具导入] 有人会说,怎么网页用JavaScript会有安全提示,而其他网站上面用了JavaScript都没有安全提示,呵呵, ...

  7. c#中类与结构的区别 struct与class的区别

    原文发布时间为:2008-11-23 -- 来源于本人的百度文章 [由搬家工具导入] 类与结构的实例比较   类与结构的差别   如何选择结构还是类   一.类与结构的示例比较:   结构示例:    ...

  8. Python学习杂记_8_从程序外部传参的办法sys.argv

    Python用 sys.argv[] 实现从程序外部传参 “外部”的含义,其实就是这些参数不是你在程序中定义的,而是在程序之外通过输入操作传递进来的.sys.argv 会返回一个元组,元组的首个元素即 ...

  9. C#使用SSDB管理增量日志并提供查询

    Program.cs using System; using System.Text; using CommonLinkLibrary.Util; using Newtonsoft.Json; nam ...

  10. Django ModelChoiceField前台下拉菜单显示object的解决方法

    在网上找了好半天都没找到,在model中添加即可,不加的话显示就是object def __str__(self): return self.name