之前的一篇文章大概讲述了一下UITableView的使用,UITableView在iOS的地位和ListView在Android中的地位基本上算是不相上下,关于ListView的优化网上的也有很多文章。UITableView苹果公司本身就已经优化了其中的功能,不管你有多少数据,每次加载的时候只是加载当前页面的数据,以免造成不必要的内存占用。一个非常常见的优化就是使用Identifier,也就是唯一标示,将页面中不用的对象放在缓存池中,如果有新的对象出现从缓存池中取出。

页面布局

页面布局还是跟上篇文章一样,一个TableView:

不过需要额外的工作的时本次暂时的是服装信息,通过弹框修改服装的价格,需要新建一个服装类,实现UIAlertViewDelegate协议,头文件中的声明:

@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

服装类的定义:

@interface Dress : NSObject

@property (strong,nonatomic) NSString *dressName;

@property (strong,nonatomic) NSString *dressDetial;

@property (strong,nonatomic) NSString  *dressImage;

@end

优化与实现  

优化之前先实现一些必要的功能,以及一些方法的使用,上篇文章只是涉及了其中的一部分,为了更好的理解,可以先看下实现的效果:

定义存储数据的数组:

@interface ViewController ()
{
NSArray *imageArr;
NSArray *dressArr;
NSMutableArray *dressList;
}
@end

初始化数据:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
imageArr=[[NSArray alloc]initWithObjects:@"dress1.jpeg",@"dress2.jpg",@"dress3.jpg",nil];
dressArr=[[NSArray alloc]initWithObjects:@"七匹狼",@"森马",@"杰克琼斯",@"美特斯邦威",@"以纯",@"真维斯",@"海南之家",nil];
dressList=[NSMutableArray arrayWithCapacity:30];
for (NSInteger i=0; i<30; i++) {
Dress *dress=[[Dress alloc]init];
NSInteger imageRandom=arc4random_uniform(3);
NSInteger dressRandom=arc4random_uniform(7); NSInteger price=arc4random_uniform(1000)+100; dress.dressImage=imageArr[imageRandom];
dress.dressName=dressArr[dressRandom];
dress.dressDetial=[NSString stringWithFormat:@"促销价:%ld",(long)price];
[dressList addObject:dress];
}
}

 设置行数:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [dressList count];
}

 设置分组:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}

 设置单元格内容,通过reuseIdentifier设置重用的单元格,dequeueReusableCellWithIdentifier取出可以重用的单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *flag=@"cacheCell";
//生成唯一的标记
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag];
if (cell==nil) {
//设置需要显示详情的样式
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:flag];
}
Dress *dress=dressList[indexPath.row]; UIImage *image=[UIImage imageNamed:dress.dressImage];
[cell.imageView setImage:image];
[cell.imageView setFrame:CGRectMake(0, 0, 80, 50)];
[cell.textLabel setText:dress.dressName];
//设置展示小箭头
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
//设置Tag
// [cell setTag:indexPath.row]; [cell.detailTextLabel setText:dress.dressDetial]; NSLog(@"获取更新之后的行:%ld",indexPath.row);
return cell;
}

选中行之后的弹框设置:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIAlertView *alterView=[[UIAlertView alloc] initWithTitle:@"服装价格:" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"修改", nil];
[alterView setAlertViewStyle:UIAlertViewStylePlainTextInput];
//选中的Cell
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath]; UITextField *textField=[alterView textFieldAtIndex:0]; //设置修改的服装信息
[textField setText:cell.detailTextLabel.text]; [textField setTag:indexPath.row];
[alterView show];
}

 UIAlterView中的点击事件:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==1) { UITextField *textField=[alertView textFieldAtIndex:0]; NSArray *selectedRow=[self.tableView indexPathsForSelectedRows]; NSIndexPath *indexPath=selectedRow[0]; Dress *dress=dressList[indexPath.row]; dress.dressDetial=textField.text; NSLog(@"%@---%ld",textField.text,(long)indexPath.row); [self.tableView reloadRowsAtIndexPaths:selectedRow withRowAnimation:UITableViewRowAnimationRight]; }
}

 设置行高:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}

关于UITable的优化:

1.最常用的就是不重复生成单元格,很常见,很实用;

2.使用不透明的视图可以提高渲染速度,xCode中默认TableCell的背景就是不透明的;

3.如果有必要减少视图中的条目,本文中设置textLabel,detialTextLabel,imageView,accessoryType;

4.更新条目的时候不要整体更新,更新选中的即可,建议reloadRowsAtIndexPaths,而不是使用reloadData;

iOS开发-UITableView表格优化的更多相关文章

  1. 李洪强iOS开发之性能优化技巧

    李洪强iOS开发之性能优化技巧 通过静态 Analyze 工具,以及运行时 Profile 工具分析性能瓶颈,并进行性能优化.结合本人在开发中遇到的问题,可以从以下几个方面进行性能优化. 一.view ...

  2. iOS开发UITableView基本使用方法总结

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

  3. iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

  4. iOS开发,UITableView相关问题

    第一条:UITableViewCell 内容的设置 //文本放到最后 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_dataArr.co ...

  5. iOS开发-UITableView自定义Cell

    UITableView在iOS中开发的重要地位是毋庸置疑的,基本上应用中用到的比例是一半左右,而且大部分情况都是需要自定义单元格的,这样用户看到的App才能更有美感.之前写过UITableView的基 ...

  6. iOS开发--项目内存优化

    在用非ARC模式编写iOS程序的时候,造成程序内存泄露在所难免,后期我们一般会进行内存优化.自己比较常用的内存优化方法有两种 1.Analyze,静态分析内存泄露的方法.很简单,在Xcode菜单栏中点 ...

  7. iOS开发 UITableView之cell

    1.cell简介 UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行 U ...

  8. iOS开发之性能优化

    1.避免过于庞大的XIB 当加载XIB的时候把所有的东西都放在了内存里,包括任何的图片:如果有一个不会即刻用到的view,就会浪费宝贵的内存资源了. 当加载一个引用了图片或者声音资源的nib时,nib ...

  9. iOS开发-UITableView常用方法

    UITableView常用来展示数据,类似于Android中的ListView,相对于Android中的ListView而言,UITableView的实现是非常简单,继承UITableViewData ...

随机推荐

  1. JQuery实现最字体的放大缩小

    网页常常有对字体放大缩小的需求,我们不妨来看一下下面这段JQuery代码的实现. 假如在html页面代码中我们有这么一段代码: <p>啦啦啦啦啦啦啦啦啦啦</p> 那么JQue ...

  2. JavaScript DOM大纲

    DOM 定义了访问和操作HTML文档的标准方法 访问(查找标签) ---- 直接查找 document.getElementById(“idname”) document.getElementsByT ...

  3. anaconda安装tensorflow后pip安装jieba出错的问题

    安装jieba出错,参考https://www.cnblogs.com/minsons/p/7872647.html TypeError: parse() got an unexpected keyw ...

  4. WinForm 使用 NPOI 2.2.1从datatable导出Excel

    最新的NOPI应该是2.3了,但在官网上还是2.2.1. 也是第一次使用NPOI来导出Excel文件. 在写的时候搜不到2.2.1的教程,搜了一个2.2.0的教程. 不过也没什么问题,NPOI是真的方 ...

  5. android activity 窗口 样式

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 将 活动 设置成 窗口样式, 可以设置 主题 为 对话框, 或者 半透明. 安卓:主题= ...

  6. [BZOJ4592][SHOI2015]脑洞治疗仪(线段树)

    线段树基础操作题,唯一需要思考下的是将区间的前k个0覆盖为1. 线段树上二分,先递归到左子树覆盖,回溯时返回还剩多少个0未被覆盖,在根据这个信息递归到右子树.注意特判k=0的情况. 要维护的信息有:区 ...

  7. 关于void main()的误区

    很多人甚至市面上的一些书籍,都使用了void main( ) ,其实这是错误的.C/C++ 中从来没有定义过void main( ) .C++ 之父 Bjarne Stroustrup 在他的主页上的 ...

  8. Android R资源文件无法更新或丢失

    开发Android应用的时候,经常容易发生R文件丢失的事. 根据我的经验是当你更新了drawable里面的文件时,正好xml文件有错误, 这样会导致R文件出错. 此时如果你clean整个project ...

  9. VS2010下配置Winpcap 开发环境

    http://blog.csdn.net/taotaoyouarebaby/article/details/27326829

  10. [Asp.net MVC]页面伪静态实现

    摘要 从页面Url及页面名称上看,你会发现静态页面和伪静态是一样的.伪静态的页面后缀可能是html,htm,cshtml等,只是改变了url的表现形式,实际上还是动态的页面.在SEO方面,伪静态和静态 ...