由于工作需要,写了一个UITableView的子类,简单的实现了每个cell的展开和收缩的动画效果以及展开和收缩后的cell样式变化。这个效果也许你现在用不到,但是它在iOS上的效果确实很不错,也许以后你就会用到。分享给大家。给大家一个实际的效果: 

<ignore_js_op> 



ExtensibleTableView.h



  1. //
  2. //  ExtensibleTableView.h
  3. //  Wow
  4. //
  5. //  Created by Boris Sun on 12-6-20.
  6. //  Copyright (c) 2012年 adsit. All rights reserved.
  7. //

  8. #import <UIKit/UIKit.h>


  9. @protocol ExtensibleTableViewDelegate <NSObject>
  10. @required
  11. //返回展开之后的cell
  12. - (UITableViewCell *)tableView:(UITableView *)tableView extendedCellForRowAtIndexPath:(NSIndexPath *)indexPath;
  13. //返回展开之后的cell的高度
  14. - (CGFloat)tableView:(UITableView *)tableView extendedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;

  15. @end

  16. @interface ExtensibleTableView : UITableView
  17. {
  18.     //当前被展开的索引
  19.     NSIndexPath *currentIndexPath;

  20.     id<ExtensibleTableViewDelegate> delegate_extend;
  21. }
  22. @property(nonatomic,retain)id delegate_extend;
  23. @property(nonatomic,retain)NSIndexPath *currentIndexPath;
  24. //将indexPath对应的row展开
  25. - (void)extendCellAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated goToTop:(BOOL)goToTop;

  26. //将展开的cell收起
  27. - (void)shrinkCellWithAnimated:(BOOL)animated;

  28. //查看传来的索引和当前被选中索引是否相同
  29. - (BOOL)isEqualToSelectedIndexPath:(NSIndexPath *)indexPath;

  30. @end



复制代码

ExtensibleTableView.m



  1. //
  2. //  ExtensibleTableView.m
  3. //  Wow
  4. //
  5. //  Created by Boris Sun on 12-6-20.
  6. //  Copyright (c) 2012年 adsit. All rights reserved.
  7. //

  8. #import "ExtensibleTableView.h"

  9. @implementation ExtensibleTableView

  10. @synthesize delegate_extend;
  11. @synthesize currentIndexPath;

  12. - (id)init
  13. {
  14.     currentIndexPath = nil;
  15.     return [super init];
  16. }

  17. //重写设置代理的方法,使为UITableView设置代理时,将子类的delegate_extend同样设置
  18. - (void)setDelegate:(id<UITableViewDelegate>)delegate
  19. {
  20.     self.delegate_extend = delegate;
  21.     [super setDelegate:delegate];
  22. }

  23. /*

  24. 将indexPath对应的row展开
  25. params:

  26. animated:是否要动画效果
  27. goToTop:展开后是否让到被展开的cell滚动到顶部

  28. */
  29. - (void)extendCellAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated goToTop:(BOOL)goToTop
  30. {      
  31.     NSLog(@"debug 2");
  32.     //被取消选中的行的索引
  33.     NSIndexPath *unselectedIndex = [NSIndexPath indexPathForRow:[currentIndexPath row] inSection:[currentIndexPath section]];
  34.     //要刷新的index的集合
  35.     NSMutableArray *array1 = [[NSMutableArray alloc]init];
  36.     //若当前index不为空
  37.     if(currentIndexPath)
  38.     {
  39.         //被取消选中的行的索引
  40.         [array1 addObject:unselectedIndex];
  41.     }

  42.     //若当前选中的行和入参的选中行不相同,说明用户点击的不是已经展开的cell
  43.     if(![self isEqualToSelectedIndexPath:indexPath])
  44.     {
  45.         //被选中的行的索引
  46.         [array1 addObject:indexPath];
  47.     }

  48.     //将当前被选中的索引重新赋值
  49.     currentIndexPath = indexPath;

  50.     if(animated)
  51.     {
  52.         [self reloadRowsAtIndexPaths:array1 withRowAnimation:UITableViewRowAnimationFade];
  53.     }
  54.     else
  55.     {
  56.         [self reloadRowsAtIndexPaths:array1 withRowAnimation:UITableViewRowAnimationNone];
  57.     }
  58.     if(goToTop)
  59.     {
  60.         //tableview滚动到新选中的行的高度
  61.         [self scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
  62.     }
  63. }

  64. //将展开的cell收起
  65. - (void)shrinkCellWithAnimated:(BOOL)animated
  66. {
  67.     //要刷新的index的集合
  68.     NSMutableArray *array1 = [[NSMutableArray alloc]init];

  69.     if(currentIndexPath)
  70.     {
  71.         //当前展开的cell的索引
  72.         [array1 addObject:currentIndexPath];
  73.         //将当前展开的cell的索引设为空
  74.         currentIndexPath = nil;
  75.         [self reloadRowsAtIndexPaths:array1 withRowAnimation:UITableViewRowAnimationFade];    
  76.     }

  77. }

  78. //查看传来的索引和当前被选中索引是否相同
  79. - (BOOL)isEqualToSelectedIndexPath:(NSIndexPath *)indexPath
  80. {
  81.     if(currentIndexPath)
  82.     {
  83.         return ([currentIndexPath row] == [indexPath row]) && ([currentIndexPath section] == [indexPath section]);
  84.     }
  85.     return NO;
  86. }

  87. /*

  88. 重写了这个方法,却无效,因为这个方法总在didSelect之前调用,很奇怪。因为无法重写该方法,所以ExtensibleTableView不算完善,因为还有额外的代码需要在heightForRowAtIndexPath和cellForRowAtIndexPath中。哪个找到完善的方法后希望可以与qq82934162联系或者在http://borissun.iteye.com来留言

  89. */

  90. //- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
  91. //{
  92. //    if([currentIndexPath row] == [indexPath row])
  93. //    {
  94. //        return [self.delegate_extend tableView:self extendedCellForRowAtIndexPath:indexPath];
  95. //    }
  96. //    return [super cellForRowAtIndexPath:indexPath];
  97. //}

  98. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  99. {
  100.     if([currentIndexPath row] == [indexPath row])
  101.     {
  102.         return [self.delegate_extend tableView:self extendedHeightForRowAtIndexPath:indexPath];
  103.     }
  104.     return [super rowHeight];
  105. }

  106. @end

复制代码

将这2个文件放到proj之后,要设置delegate_extend并且实现 

//返回展开之后的cell 

- (UITableViewCell *)tableView:(UITableView *)tableView extendedCellForRowAtIndexPath:(NSIndexPath *)indexPath; 

//返回展开之后的cell的高度 

- (CGFloat)tableView:(UITableView *)tableView extendedHeightForRowAtIndexPath:(NSIndexPath *)indexPath; 

2个方法。 


还有一点不合理的地方,我试着去解决,但是最终未果=。=!



  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3.     //若当前行被选中,则返回展开的cell
  4.     if([tableView_ isEqualToSelectedIndexPath:indexPath])
  5.     {
  6.         return [self tableView:tableView extendedCellForRowAtIndexPath:indexPath];
  7.     }
  8.     ...
  9. }

复制代码

这里要先判断当前行是否被选中,若被选中则调用extendedCellForRowAtIndexPath方法。因为我试着重写UITableView的- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath方法。试图在这个方法里做上边的事情,可是这个方法总是在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法之前被调用,因此没有达到预期的目标。