应用程序之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系统——后缀为.zip.00X的zip分卷解压
Windows下后缀为*.zip.001文件的解压方法: 后缀为*.zip.001文件用winrar无法解压, 解决办法是在windows下打开命令行界面, 输入:copy /B xx.zip.001 ...
- 刷题总结——次小生成树(bzoj1977 最小生成树+倍增)
题目: Description 小 C 最近学了很多最小生成树的算法,Prim 算法.Kurskal 算法.消圈算法等等. 正当小 C 洋洋得意之时,小 P 又来泼小 C 冷水了.小 P 说,让小 C ...
- sql 学习相关问题
---恢复内容开始--- 1.sql上面改变列的数据类型是 ALTER TABLE table_nameALTER COLUMN column_name datatype mysql上面是ALTER ...
- 使用libcurl提示 LNK2001的错误
vs使用libcurl(static library),link时报错: error LNK2001: unresolved external symbol __imp__curl_easy_perf ...
- 几个实用的 jQuery 插件
1. owl.carousel -- 强大实用的jQuery幻灯片插件 2. jquery.nicescroll.min.js -- 自定义滚动条样式,支持 div,iframe,html 等. 3. ...
- js 清空div
document.getElementById('BIGDraw').innerHTML = ""; $('#BIGDraw').html(""); $('#B ...
- C# Log日志工具类
using System; using System.Collections.Generic; using System.Text; using System.IO; public class Log ...
- linux程序运行浅析
例如有一个脚本文件tests.sh,内容如下: #!/bin/bash #This is a sample test. cd /tmp echo "Hello, this is a test ...
- EBImage - - 给图片增加字符
EBImage中文文档 英文版出处:http://www.bioconductor.org/packages/release/bioc/vignettes/EBImage/inst/doc/EBIma ...
- C# Stopwatch详解
namespace System.Diagnostics { // // 摘要: // 提供一组方法和属性,可用于准确地测量运行时间. public class Stopwatch { // // 摘 ...