iOS开发-UITableView表格优化

十度 IOS 2015年12月01日 收藏

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

页面布局

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

 

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

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

服装类的定义:

  1. @interface Dress : NSObject
  2.  
  3. @property (strong,nonatomic) NSString *dressName;
  4.  
  5. @property (strong,nonatomic) NSString *dressDetial;
  6.  
  7. @property (strong,nonatomic) NSString *dressImage;
  8.  
  9. @end

优化与实现  

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

定义存储数据的数组:

 

  1. @interface ViewController ()
  2. {
  3. NSArray *imageArr;
  4. NSArray *dressArr;
  5. NSMutableArray *dressList;
  6. }
  7. @end

 

初始化数据:

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

 设置行数:

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

 设置分组:

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

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

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

选中行之后的弹框设置:

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

 UIAlterView中的点击事件:

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

 设置行高:

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

关于UITable的优化:

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

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

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

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