应用程序之UITableView的Plain用法和cell缓存池优化
- 效果展示
- 过程分析
- 代码实现
- cell缓存池优化
一、效果展示



二、过程分析
- 首先通过三步创建数据,展示数据
- 监听选中某一个cell时调用的方法
- 在cell中创建一个对话框
- 修改对话框中的值,并且重新刷新选中的那个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缓存池优化

- 用到时创建(苹果已经帮我们完成)
- 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缓存池优化的更多相关文章
- UITableView的常用属性和cell的内存优化
UITableView的常用属性: 分割线颜色设置: 1> 设置separatorStyle: 分割线的颜色 方法:tableView.separatorStyle = UITableViewC ...
- UITableView系列(1)---Apple缓存池机制
一.概述 关于UITableView的基本使用, 其实十分简单.但是做App最重要的之一就是细致,技术方面要做到细致, 必须深入了解底层, 才能做出优化让程序跑得更快.那么这一系列文章从我实际项目中获 ...
- iOS - UITableView中有两种重用Cell的方法
UITableView中有两种重用Cell的方法: - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; - (id)dequ ...
- 程序员收藏必看系列:深度解析MySQL优化(二)
程序员收藏必看系列:深度解析MySQL优化(一) 性能优化建议 下面会从3个不同方面给出一些优化建议.但请等等,还有一句忠告要先送给你:不要听信你看到的关于优化的“绝对真理”,包括本文所讨论的内容,而 ...
- 微信小程序开发:学习笔记[9]——本地数据缓存
微信小程序开发:学习笔记[9]——本地数据缓存 快速开始 说明 本地数据缓存是小程序存储在当前设备上硬盘上的数据,本地数据缓存有非常多的用途,我们可以利用本地数据缓存来存储用户在小程序上产生的操作,在 ...
- 程序设计模式浅析(plain framework商业版设计模式)
程序设计其实对程序开发者来说十分重要,但是在工作中往往我们却忽略了这一块,因为我们所用的都是现有的模式.一个设计模式的好坏,往往能够体现出程序的专业性,还有整个项目的可持续性.这就是为什么有些公司,在 ...
- iOS 7 UITableview 在Plain模式下 设置背景颜色无效
在iOS6的时候,设置Plain模式下table的颜色 通过[self.table setBackgroundColor:[UIColor red]]; 就可以看到一个满身通红的tableView 但 ...
- 应用程序之UITableView的编辑模式
cell分层结构 效果展示 代码实现 一.cell的分层结构 二.效果展示 三.代码实现 // // ViewController.m // 01-TableView的删除实现 // // Creat ...
- UITableView的plain样式下,取消区头停滞效果
核心代码 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat sectionHeaderHeight = sectionH ...
随机推荐
- windows通过NSF挂载centos目录
windows通过NSF挂载centos目录 来源: http://blog.csdn.net/u012955038/article/details/77151346 CentOS 配置 1.安装NF ...
- hdu 3721 树的直径
思路:枚举+树的直径 #include<iostream> #include<cstring> #include<cstdio> #include<algor ...
- wewe
<#assign base=rc.contextPath /> <#import "spring.ftl" as s /> <!DOCTYPE htm ...
- python百度贴吧爬虫
# -*- coding: utf-8 -*- #coding=utf-8 import urllib import urllib2 import re import thread import ti ...
- 【转】 #define用法详解
#define用法详解 1.#define 的作用 在C或C++语言源程序中允许用一个标识符来表示一个字符串,称为“宏”.被定义为“宏”的标识符称为“宏名”.在编译预处理时,对程序中所有出现的“宏 ...
- JavaScript Array 对象的方法,比如push和unshift
https://www.runoob.com/jsref/jsref-obj-array.html js数组与字符串的相互转换 一.数组转字符串 需要将数组元素用某个字符连接成字符串,示例代码如下: ...
- tomcat defaultServlet
首先所有的请求进入tomcat,都会流经servlet,如果没有匹配到任何应用指定的servlet,那么就会流到默认的servlet. 默认的servlet是配置在$catalina/conf/web ...
- Java数据结构-------List
三种List:ArrayList,Vector,LinkedList 类继承关系图 ArrayList和Vector通过数组实现,几乎使用了相同的算法:区别是ArrayList不是线程安全的,Vect ...
- Feeling kind of the sorrow
It's almost a long time, in this place, but sometimes, feelings do vary. When I stepped in front of ...
- poj 3311 Hie with the Pie 经过所有点(可重)的最短路径 floyd + 状压dp
题目链接 题意 给定一个\(N\)个点的完全图(有向图),求从原点出发,经过所有点再回到原点的最短路径长度(可重复经过中途点). 思路 因为可多次经过同一个点,所以可用floyd先预处理出每两个点之间 ...