• 效果展示
  • 过程分析
  • 代码实现
  • 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. linux系统初始化——inittab文件解析

    inittab文件解析 inittab配置文件每行的基本格式如下. id:runlevels:action:process 其中某些部分可以为空,下面我们逐一介绍. 1.id 1-2个字符,配置行的唯 ...

  2. serviceImpl中,方法加@Override注释后报错

    @Override public List<SysAdminMenu> getAdminMenusAll() { return sysAdminMenuMapper.getAdminMen ...

  3. java递归处理文件夹和文件

    import java.io.File; /** * 文件综合使用示例 */ public class FileDelete { public static void main(String[] ar ...

  4. 51 Nod 1678 lyk与gcd

    1678 lyk与gcd 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 这天,lyk又和gcd杠上了.它拥有一个n个数的数列,它想实现两种操作. 1:将  ai  ...

  5. 【HDOJ5556】Land of Farms(最大团)

    题意:给定n*m的网格图,上面只有字符'.' 和 数字0-9.其中数字表示这是该格是古老的土地,字符'.'表示该格只是普通的土地. 可以认为一块古老的农田由四联通的所有数字相同的格组成的块,一块普通的 ...

  6. cookie中存储json

    原文发布时间为:2009-12-14 -- 来源于本人的百度文章 [由搬家工具导入] http://www.denisdeng.com/?p=563 最近的一个项目需要在cookie中存储json对象 ...

  7. [LeetCode] Balanced Binary Tree 深度搜索

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  8. MySql授权和撤销权限操作

    MySQL 赋予用户权限命令的简单格式可概括为: grant 权限 on 数据库对象 to 用户 用户后面可以加@'ip地址' identified by '密码' 例如: ' ' 上面的语句表示将 ...

  9. C++ 求幂的运算符是什么?

    1.VB里面求幂的运算符是“^” 2.C++没有求幂的运算符, c++头文件加 #include<math.h>使用pow(x,y),可算出x的y次幂 3.C++中 “^”是按位“异或”运 ...

  10. Elasticsearch之pythonAPI简单使用

    elasticsearch自动补全建议功能 数据入库操作 ESmapping要求 PUT music { "mappings": { "_doc" : { &q ...