您的位置:首页 > 产品设计 > UI/UE

UI—UITableView的性能优化、plist文件的加载

2015-03-05 23:47 453 查看
一、性能优化

能看见多少UITableViewCell就创建多少cell,利用在创建UITalbeVIewCell的时候传递一个reuseIndentifier来进行cell的重复利用。当需要一个cell的时候去缓存池找。找不到就创建。

staticNSString
*idValue =@"12345”;只会分配一次存储空间。
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:idValue];先去缓存池取,tableView是协议方法传进来的。

if (cell ==nil) {

cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:idValue];设置表示符,找不到就创建新的。想查看是否有重用,看对象地址。

}

二、淘宝应用
1、plist文件来取得数据,plist文件里面创建一个数组、数组里面在放入字典。
2、搭建界面。UIToolBar 里面只能放只能放UIBarButtonItem,item有个indetifier属性,可以设置item的样式,比如trash,伸缩条等。但同时又有一个style属性,这个样式有plain无边框,bordered有边框,done。可能style更倾向于模型上的样式,indetifier倾向于含义,内在上的东西。文本框里的内容叫text。其框上显示的字都叫title。tint颜色是字面的颜色和backgroud有区别。
3、搭建界面。导航条下面放一个UITableVIew,同时设置控制器为代理,让控制器文件遵守协议。采用拖拽形式,可以省着设置frame,也不用加到父控件内,容果想拥有这个控件,拖成控制器的属性即可。
4、实现协议中必须实现的方法。单组为例,一共有多少行,每一行的内容。创建UITableVIewCell对象,从tableView缓存池中取,取不到创建,注意static的标示符。
5、加载数据,数据只加载一次,就在VIewDidLoad方法中写。既然plist文件里装的是数组,那么一定是调用array方法来获取这个数组。在吧数组中的字典转换成模型对象,这样才符合面向对象方法。

NSString
*path = [[NSBundlemainBundle]pathForResource:@"shop"ofType:@“plist"];

NSArray *array = [NSArrayarrayWithContentsOfFile:path];

6、之后根据模型的数据,设置一共有多少行,在设置cell的内容,cell中内置两个UIlable和一个uiiamgeVIew还有一个小图标。把数据内射即可。

7、通过代理方法,可以设置每一行的高度,具体代码可参见前几节笔记。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath

8、要实现在界面上选中某一行cell,如何实现。默认情况下,选中某一行变蓝,但想要选中是有个打勾的图标,则需要拿到这行cell,设置这行的accessoryType属性.其次用代理方法,监听界面点击事件。下面方法只有tableview属性,没有UItableVIewCell,但tableView里面应该有方法拿到。解决。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath;

UITableViewCell *cell = [tableViewcellForRowAtIndexPath:indexPath];

[tableView deselectRowAtIndexPath:indexPathanimated:YES];取消选中某一行,选中会有默认的蓝色背景,取消这个背景,这个方法和要实现的选中不是一回事。
9、上面可以让cell实现打勾效果不合理,首先不符合MVC,要改变数据来改变状态,而不是改模型,其次由于缓存池功能,重复利用的cell也会被改动。失败。解决方法2.通过数据来改变模型。3选中那一行就把那一行的模型数据装进数组中去。然后重新加载cell,方法里判读是否有这个模型数据,有则打勾,没有则取消打勾。
10、UIBarButtonItem上面的文字不可以修改。只能在uitoolBar覆盖一个UIlable来改文字的值。让一个uilabe剧中,最好让其完全覆盖在一个控件上面,通过alignment设置居中属性。注入这个属性,然后改变其文字值即可,在没错重载的时候修改,活在点击触发事件中修改都可以。
11、事件触发控件具有穿透性。ibaction iboutlet
12、删除选中数据的实现方法,UIBarButtonItem可以通过拖线实现方法调用。enable属性能控制其是否能点击,虽然是BarButoonItem但也相当于一个按钮。实现思路:删除数据,把数组中的数据删除。刷新表格通过uiTableVIew来删除,没有则注入。

#import "ViewController.h"

#import "Data.h"

@interface
ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray *_modelArray;
NSMutableArray *_delArray;
}

@end

@implementation ViewController

- (void)viewDidLoad {

[superviewDidLoad];

NSString *path = [[NSBundlemainBundle]
pathForResource:@"shops"ofType:@"plist"];
NSArray *array = [NSArrayarrayWithContentsOfFile:path];

_modelArray = [NSMutableArrayarray];
[arrayenumerateObjectsUsingBlock:^(id obj,NSUInteger idx,
BOOL *stop) {
Data *data = [[Dataalloc]
initWithDictionary:obj];
[_modelArrayaddObject:data];
}];

_delArray = [NSMutableArrayarray];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return_modelArray.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
static
NSString *idOnly =@"idOnly";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:idOnly];
if (cell ==
nil) {

cell = [[UITableViewCellalloc]
initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:idOnly];
}
Data *data =
_modelArray[indexPath.row];
cell.textLabel.text = data.name;
cell.detailTextLabel.text = data.desc;
cell.imageView.image = [UIImageimageNamed:data.icon];
if ([_delArraycontainsObject:indexPath]) {

cell.accessoryType =UITableViewCellAccessoryCheckmark;
}else{

cell.accessoryType =UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath{
if ([_delArraycontainsObject:indexPath]) {
[_delArrayremoveObject:indexPath];
}else{
[_delArrayaddObject:indexPath];
}

[tableView deselectRowAtIndexPath:indexPathanimated:YES];

[tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationBottom];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: