使用HVTableView动态展开tableView中的cell

效果:

源码:

HVTableView.h 与 HVTableView.m

  1. //
  2. // HVTableView.h
  3. // HRVTableView
  4. //
  5. // Created by Hamidreza Vakilian on 25/11/2013
  6. // Copyright (c) 2013 Hamidreza Vakilian. All rights reserved.
  7. // Website: http://www.infracyber.com/
  8. // Email: xerxes235@yahoo.com
  9. //
  10.  
  11. //
  12. //*************** HVTableView - UITableView with expand/collapse feature *****************************
  13. // by hamidreza vakilian
  14. //
  15. //
  16. // This is a subclass of UITableView with expand/collapse feature that may come so handy in many apps.
  17. // The developer can save a lot of time using an expand/collapse tableView instead of creating a detail viewController for
  18. // every cell. I mean the details of each cell can be displayed immediately on the same table without switching to
  19. // another view. On the other hand in my opinion it far more impressive and eye-catching from a regular user's view.
  20. //
  21. // To create an instance of HVTableView you go by code.
  22. // (currently I you can't do it from xib - I will work on that later)
  23. // That's simple as:
  24. //
  25. // HVTableView* myTable = [[HVTableView alloc] initWithFrame:CGRectMake(84, 250, 600, 600) expandOnlyOneCell:NO enableAutoScroll:YES];
  26. // myTable.HVTableViewDelegate = self;
  27. // myTable.HVTableViewDataSource = self;
  28. // [myTable reloadData];
  29. // [self.view addSubview:myTable];
  30.  
  31. // Two important parameters when initializing the HVTableView
  32. // if expandOnlyOneCell==TRUE: Just one cell will be expanded at a time.
  33. // if expandOnlyOneCell==FALSE: multiple cells can be expanded at a time
  34. //
  35. // if enableAutoScroll==TRUE: when the user touches a cell, the HVTableView will automatically scroll to it
  36. // if enableAutoScroll==FALSE: when the user touches a cell, the HVTableView won't scroll. but the if the cell was close to the bottom of the table; the lower part of it may go invisible because it grows
  37. //
  38. //
  39. // Your viewController must conform to HVTableViewDelegate, HVTableViewDataSource protocols. Just like the regular
  40. // UITableView
  41. //
  42. // Like before you implement these familiar delegate functions:
  43. // -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  44. // -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  45. //
  46. // I added a boolean parameter the heightForRowAtIndexPath function so you will return different values for an expanded or a collapsed cell.
  47. // (isExpanded==TRUE: return the size of the cell in expanded state)
  48. // (isExpanded==FALSE: return the size of the cell in collapsed (initial) state)
  49. //
  50. // -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath isExpanded:(BOOL)isexpanded
  51. //
  52. //
  53. // I also added a boolean parameter to the cellForRowAtIndexPath function too. update the cell's content respecting it's state (isExpanded)
  54. // -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath isExpanded:(BOOL)isExpanded
  55. //
  56. //
  57. // Now the interesting functions are here. Implement this function and it will be fired when a cell is going to expand. You can perform your drawings, animations, etc. in this function:
  58. // -(void)tableView:(UITableView *)tableView collapseCell: (UITableViewCell*)cell withIndexPath:(NSIndexPath*) indexPath;
  59. //
  60. // Implement this function. it will be fired when a cell is going to collapse. You can perform your drawings, animations, etc. or clearing up the cell in this function:
  61. // -(void)tableView:(UITableView *)tableView expandCell: (UITableViewCell*)cell withIndexPath:(NSIndexPath*) indexPath;
  62. //
  63. // IMPORTANT NOTE: there are some delegate functions from UITableViewDelegate that I have commented their forwarding. If you need to implement those on your viewController or smth, go to HVTableView.m and uncomment those delegate functions. If you don't uncomment them; your delegate functions won't fire up.
  64. //
  65. //
  66. // This code may contain bugs. I don't garauntee its functionality, but use it on your own risk. I also tried to craft it with best performance, yet it can be optimized more.
  67. //
  68. //
  69. // Please don't hesitate to mail me your feedbacks and suggestions or a bug report. I would be very thankful of your responses.
  70. //
  71. // ON THE BOTTOM LINE: I allow you to use my code in your applications with freedom in making any modifications, but if you are going to do so, or you just like it and want further updates and bug fixes please consider donating me via this url:
  72. // https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=xerxes235%40yahoo%2ecom&lc=AE&item_name=Hamidreza%20Vakilian&item_number=HVTableView%20donation&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted
  73. //
  74. //
  75. // Thanks,....
  76. // ***************************************************************************************************************
  77.  
  78. #import <UIKit/UIKit.h>
  79.  
  80. @protocol HVTableViewDataSource <NSObject>
  81. @required
  82. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
  83. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath isExpanded:(BOOL)isExpanded;
  84. -(void)tableView:(UITableView *)tableView collapseCell: (UITableViewCell*)cell withIndexPath:(NSIndexPath*) indexPath;
  85. -(void)tableView:(UITableView *)tableView expandCell: (UITableViewCell*)cell withIndexPath:(NSIndexPath*) indexPath;
  86. @optional
  87.  
  88. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
  89. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
  90. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
  91. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
  92. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
  93. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
  94. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;
  95. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
  96. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
  97. @end
  98.  
  99. @protocol HVTableViewDelegate <NSObject>
  100. @optional
  101. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
  102. - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
  103. - (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
  104. - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0);
  105. - (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
  106. - (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
  107. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath isExpanded:(BOOL)isExpanded;
  108. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
  109. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
  110. - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0);
  111. - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
  112. - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
  113. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
  114. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
  115. - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath NS_DEPRECATED_IOS(2_0, 3_0);
  116. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
  117. - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
  118. - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
  119. - (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
  120. - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
  121. - (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
  122. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
  123. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
  124. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
  125. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
  126. - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
  127. - (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;
  128. - (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath;
  129. - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;
  130. - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath;
  131. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0);
  132. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0);
  133. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0);
  134. @end
  135.  
  136. @interface HVTableView : UITableView <UITableViewDataSource, UITableViewDelegate>
  137. {
  138.  
  139. NSIndexPath *selectedIndexPath;
  140. int actionToTake;
  141. NSMutableArray* expandedIndexPaths;
  142. }
  143.  
  144. @property (weak,nonatomic) id <HVTableViewDelegate> HVTableViewDelegate;
  145. @property (weak,nonatomic) id <HVTableViewDataSource> HVTableViewDataSource;
  146. @property (nonatomic) BOOL expandOnlyOneCell;
  147. @property (nonatomic) BOOL enableAutoScroll;
  148.  
  149. - (id)initWithFrame:(CGRect)frame
  150. expandOnlyOneCell:(BOOL)expandOnlyOneCell
  151. enableAutoScroll:(BOOL)enableAutoScroll;
  152.  
  153. @end
  1. //
  2. // HVTableView.m
  3. // HRVTableView
  4. //
  5. // Created by Hamidreza Vakilian on 25/11/2013
  6. // Copyright (c) 2013 Hamidreza Vakilian. All rights reserved.
  7. // Website: http://www.infracyber.com/
  8. // Email: xerxes235@yahoo.com
  9. //
  10.  
  11. #import "HVTableView.h"
  12.  
  13. @implementation HVTableView
  14. @synthesize HVTableViewDelegate, HVTableViewDataSource;
  15.  
  16. - (id)initWithCoder:(NSCoder *)aDecoder {
  17. self = [super initWithCoder:aDecoder];
  18. if(self) {
  19. self.delegate = self;
  20. self.dataSource = self;
  21. expandedIndexPaths = [NSMutableArray new];
  22. }
  23. return self;
  24. }
  25.  
  26. - (id)initWithFrame:(CGRect)frame expandOnlyOneCell:(BOOL)expandOnlyOneCell enableAutoScroll: (BOOL)enableAutoScroll
  27. {
  28. self = [super initWithFrame:frame];
  29. if (self) {
  30. // Initialization code
  31. _expandOnlyOneCell = expandOnlyOneCell;
  32. if (!_expandOnlyOneCell)
  33. expandedIndexPaths = [[NSMutableArray alloc] init];
  34.  
  35. _enableAutoScroll = enableAutoScroll;
  36.  
  37. self.delegate = self;
  38. self.dataSource = self;
  39.  
  40. }
  41. return self;
  42. }
  43.  
  44. //////// IMPORTANT!!!!!!!!!!!!!!!!!!!!!
  45. //////// UITableViewDataSource Protocol Forwarding
  46. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  47. {
  48. return [HVTableViewDataSource tableView:tableView numberOfRowsInSection:section];
  49. }
  50.  
  51. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  52. {
  53. return [HVTableViewDataSource numberOfSectionsInTableView:tableView];
  54. }
  55.  
  56. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  57. {
  58. if ([HVTableViewDataSource respondsToSelector:@selector(tableView:titleForHeaderInSection:)])
  59. return [HVTableViewDataSource tableView:tableView titleForHeaderInSection:section];
  60. return nil;
  61. }
  62.  
  63. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
  64. { if ([HVTableViewDataSource respondsToSelector:@selector(tableView:titleForFooterInSection:)])
  65. return [HVTableViewDataSource tableView:tableView titleForFooterInSection:section];
  66. return nil;
  67. }
  68.  
  69. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  70. { if ([HVTableViewDataSource respondsToSelector:@selector(tableView:canEditRowAtIndexPath:)])
  71. return [HVTableViewDataSource tableView:tableView canEditRowAtIndexPath:indexPath];
  72. return NO;
  73. }
  74.  
  75. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  76. {
  77. if ([HVTableViewDataSource respondsToSelector:@selector(tableView:canMoveRowAtIndexPath:)])
  78. return [HVTableViewDataSource tableView:tableView canMoveRowAtIndexPath:indexPath];
  79. return NO;
  80. }
  81.  
  82. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
  83. {
  84. if ([HVTableViewDataSource respondsToSelector:@selector(sectionIndexTitlesForTableView:)])
  85. return [HVTableViewDataSource sectionIndexTitlesForTableView:tableView];
  86. return nil;
  87. }
  88.  
  89. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
  90. {
  91. if ([HVTableViewDataSource respondsToSelector:@selector(tableView:sectionForSectionIndexTitle:atIndex:)])
  92. return [HVTableViewDataSource tableView:tableView sectionForSectionIndexTitle:title atIndex:index];
  93. return ;
  94. }
  95.  
  96. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  97. {
  98. if ([HVTableViewDataSource respondsToSelector:@selector(tableView:commitEditingStyle:forRowAtIndexPath:)])
  99. return [HVTableViewDataSource tableView:tableView commitEditingStyle:editingStyle forRowAtIndexPath:indexPath];
  100. }
  101.  
  102. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
  103. {
  104. if ([HVTableViewDataSource respondsToSelector:@selector(tableView:moveRowAtIndexPath:toIndexPath:)])
  105. return [HVTableViewDataSource tableView:tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
  106. }
  107.  
  108. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  109. {
  110. UITableViewCell* cell;
  111.  
  112. if (self.expandOnlyOneCell)
  113. {
  114. if (actionToTake == ) // e.g. the first time or an expanded cell from before gets in to view
  115. {
  116. if (selectedIndexPath)
  117. if (selectedIndexPath.row == indexPath.row && selectedIndexPath.section == indexPath.section)
  118. {
  119. cell = [HVTableViewDataSource tableView:tableView cellForRowAtIndexPath:indexPath isExpanded:YES];//i want it expanded
  120. return cell;
  121. }
  122.  
  123. cell = [HVTableViewDataSource tableView:tableView cellForRowAtIndexPath:indexPath isExpanded:NO];
  124.  
  125. return cell; //it's already collapsed!
  126. }
  127.  
  128. cell = [HVTableViewDataSource tableView:tableView cellForRowAtIndexPath:indexPath isExpanded:NO];
  129.  
  130. if(actionToTake == -)
  131. {
  132. [HVTableViewDataSource tableView:tableView collapseCell:cell withIndexPath:indexPath];
  133. actionToTake = ;
  134. }
  135. else
  136. {
  137. [HVTableViewDataSource tableView:tableView expandCell:cell withIndexPath:indexPath];
  138. actionToTake = ;
  139. }
  140. }
  141. else
  142. {
  143. if (actionToTake == ) // e.g. the first time or an expanded cell from before gets in to view
  144. {
  145. BOOL alreadyExpanded = NO;
  146. NSIndexPath* correspondingIndexPath;
  147. for (NSIndexPath* anIndexPath in expandedIndexPaths) {
  148. if (anIndexPath.row == indexPath.row && anIndexPath.section == indexPath.section)
  149. {alreadyExpanded = YES; correspondingIndexPath = anIndexPath;}
  150. }
  151.  
  152. if (alreadyExpanded)
  153. cell = [HVTableViewDataSource tableView:tableView cellForRowAtIndexPath:indexPath isExpanded:YES];
  154. else
  155. cell = [HVTableViewDataSource tableView:tableView cellForRowAtIndexPath:indexPath isExpanded:NO];
  156.  
  157. return cell; //it's already collapsed!
  158.  
  159. }
  160.  
  161. cell = [HVTableViewDataSource tableView:tableView cellForRowAtIndexPath:indexPath isExpanded:NO];
  162.  
  163. if(actionToTake == -)
  164. {
  165. [HVTableViewDataSource tableView:tableView collapseCell:cell withIndexPath:indexPath];
  166. actionToTake = ;
  167. }
  168. else
  169. {
  170. [HVTableViewDataSource tableView:tableView expandCell:cell withIndexPath:indexPath];
  171. actionToTake = ;
  172. }
  173. }
  174.  
  175. return cell;
  176. }
  177.  
  178. //////// IMPORTANT!!!!!!!!!!!!!!!!!!!!!
  179. //////// IMPORTANT!!!!!!!!!!!!!!!!!!!!!
  180. //////// IMPORTANT!!!!!!!!!!!!!!!!!!!!!
  181. //////// IMPORTANT!!!!!!!!!!!!!!!!!!!!!
  182. //////// IMPORTANT!!!!!!!!!!!!!!!!!!!!!
  183. //////// UITableViewDelegate Protocol Forwarding
  184. //////// NOTE::::: If you want to use any of these protocols in your code; you must uncomment it. I have comment them for performance reasons
  185. /*
  186. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  187. {
  188. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:willDisplayCell:forRowAtIndexPath:)])
  189. [HVTableViewDelegate tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
  190. }
  191.  
  192. -(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
  193. {
  194. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:willDisplayHeaderView:forSection:)])
  195. [HVTableViewDelegate tableView:tableView willDisplayHeaderView:view forSection:section];
  196. }
  197.  
  198. -(void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
  199. {
  200. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:willDisplayFooterView:forSection:)])
  201. [HVTableViewDelegate tableView:tableView willDisplayFooterView:view forSection:section];
  202. }
  203.  
  204. -(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  205. {
  206. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:didEndDisplayingCell:forRowAtIndexPath:)])
  207. [HVTableViewDelegate tableView:tableView didEndDisplayingCell:cell forRowAtIndexPath:indexPath];
  208. }
  209.  
  210. - (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section
  211. {
  212. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:didEndDisplayingHeaderView:forSection:)])
  213. [HVTableViewDelegate tableView:tableView didEndDisplayingHeaderView:view forSection:section];
  214. }
  215.  
  216. - (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section
  217. {
  218. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:didEndDisplayingFooterView:forSection:)])
  219. [HVTableViewDelegate tableView:tableView didEndDisplayingFooterView:view forSection:section];
  220. }
  221. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  222. {
  223. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:heightForHeaderInSection:)])
  224. return [HVTableViewDelegate tableView:tableView heightForHeaderInSection:section];
  225. return 0;
  226. }
  227.  
  228. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  229. {
  230. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:heightForFooterInSection:)])
  231. return [HVTableViewDelegate tableView:tableView heightForFooterInSection:section];
  232. return 0;
  233. }
  234.  
  235. - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0)
  236. {
  237. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:estimatedHeightForRowAtIndexPath:)])
  238. return [HVTableViewDelegate tableView:tableView estimatedHeightForRowAtIndexPath:indexPath];
  239. return 100;
  240. }
  241.  
  242. - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0)
  243. {
  244. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:estimatedHeightForHeaderInSection:)])
  245. return [HVTableViewDelegate tableView:tableView estimatedHeightForHeaderInSection:section];
  246. return 0;
  247. }
  248.  
  249. - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
  250. {
  251. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:estimatedHeightForFooterInSection:)])
  252. return [HVTableViewDelegate tableView:tableView estimatedHeightForFooterInSection:section];
  253. return 0;
  254. }
  255.  
  256. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  257. {
  258. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:viewForHeaderInSection:)])
  259. return [HVTableViewDelegate tableView:tableView viewForHeaderInSection:section];
  260. return nil;
  261. }
  262. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  263. {
  264. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:viewForFooterInSection:)])
  265. return [HVTableViewDelegate tableView:tableView viewForFooterInSection:section];
  266. return nil;
  267. }
  268.  
  269. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
  270. {
  271. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:accessoryButtonTappedForRowWithIndexPath:)])
  272. [HVTableViewDelegate tableView:tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
  273. }
  274.  
  275. - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
  276. {
  277. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:shouldHighlightRowAtIndexPath:)])
  278. return [HVTableViewDelegate tableView:tableView shouldHighlightRowAtIndexPath:indexPath];
  279. return YES;
  280. }
  281. - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
  282. {
  283. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:didHighlightRowAtIndexPath:)])
  284. [HVTableViewDelegate tableView:tableView didHighlightRowAtIndexPath:indexPath];
  285. }
  286. - (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath
  287. {
  288. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:didUnhighlightRowAtIndexPath:)])
  289. [HVTableViewDelegate tableView:tableView didUnhighlightRowAtIndexPath:indexPath];
  290. }
  291. - (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
  292. {
  293. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:willDeselectRowAtIndexPath:)])
  294. return [HVTableViewDelegate tableView:tableView willDeselectRowAtIndexPath:indexPath];
  295. return nil;
  296. }
  297.  
  298. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
  299. {
  300. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:didDeselectRowAtIndexPath:)])
  301. [HVTableViewDelegate tableView:tableView didDeselectRowAtIndexPath:indexPath];
  302. }
  303. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  304. {
  305. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:editingStyleForRowAtIndexPath:)])
  306. return [HVTableViewDelegate tableView:tableView editingStyleForRowAtIndexPath:indexPath];
  307. return UITableViewCellEditingStyleNone;
  308. }
  309. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  310. {
  311. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:)])
  312. return [HVTableViewDelegate tableView:tableView titleForDeleteConfirmationButtonForRowAtIndexPath:indexPath];
  313. return nil;
  314. }
  315. - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
  316. {
  317. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:shouldIndentWhileEditingRowAtIndexPath:)])
  318. return [HVTableViewDelegate tableView:tableView shouldIndentWhileEditingRowAtIndexPath:indexPath];
  319. return NO;
  320. }
  321. - (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
  322. {
  323. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:willBeginEditingRowAtIndexPath:)])
  324. [HVTableViewDelegate tableView:tableView willBeginEditingRowAtIndexPath:indexPath];
  325. }
  326. - (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
  327. {
  328. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:didEndEditingRowAtIndexPath:)])
  329. [HVTableViewDelegate tableView:tableView didEndEditingRowAtIndexPath:indexPath];
  330. }
  331. - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
  332. {
  333. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:)])
  334. return [HVTableViewDelegate tableView:tableView targetIndexPathForMoveFromRowAtIndexPath:sourceIndexPath toProposedIndexPath:proposedDestinationIndexPath];
  335. return nil;
  336. }
  337. - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
  338. {
  339. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:indentationLevelForRowAtIndexPath:)])
  340. return [HVTableViewDelegate tableView:tableView indentationLevelForRowAtIndexPath:indexPath];
  341. return 0;
  342. }
  343. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
  344. {
  345. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:shouldShowMenuForRowAtIndexPath:)])
  346. return [HVTableViewDelegate tableView:tableView shouldShowMenuForRowAtIndexPath:indexPath];
  347. return YES;
  348. }
  349. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  350. {
  351. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:canPerformAction:forRowAtIndexPath:withSender:)])
  352. return [HVTableViewDelegate tableView:tableView canPerformAction:action forRowAtIndexPath:indexPath withSender:sender];
  353. return NO;
  354. }
  355. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  356. {
  357. if ([HVTableViewDelegate respondsToSelector:@selector(tableView:performAction:forRowAtIndexPath:withSender:)])
  358. return [HVTableViewDelegate tableView:tableView performAction:action forRowAtIndexPath:indexPath withSender:sender];
  359. }
  360. */
  361.  
  362. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  363. {
  364. if (self.expandOnlyOneCell)
  365. {
  366. if (selectedIndexPath)
  367. if(selectedIndexPath.row == indexPath.row && selectedIndexPath.section == indexPath.section)
  368. return [HVTableViewDelegate tableView:tableView heightForRowAtIndexPath:indexPath isExpanded:YES];
  369.  
  370. return [HVTableViewDelegate tableView:tableView heightForRowAtIndexPath:indexPath isExpanded:NO];
  371. }
  372. else
  373. {
  374. BOOL alreadyExpanded = NO;
  375. NSIndexPath* correspondingIndexPath;
  376. for (NSIndexPath* anIndexPath in expandedIndexPaths) {
  377. if (anIndexPath.row == indexPath.row && anIndexPath.section == indexPath.section)
  378. {alreadyExpanded = YES; correspondingIndexPath = anIndexPath;}
  379. }
  380. if (alreadyExpanded)
  381. return [HVTableViewDelegate tableView:tableView heightForRowAtIndexPath:indexPath isExpanded:YES];
  382. else
  383. return [HVTableViewDelegate tableView:tableView heightForRowAtIndexPath:indexPath isExpanded:NO];
  384. }
  385. }
  386.  
  387. -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
  388. {
  389. return indexPath;
  390. }
  391.  
  392. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  393. {
  394. // 更新代理,让外面的tableView也可以使用
  395. if (self.HVTableViewDelegate && [self.HVTableViewDelegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
  396. [self.HVTableViewDelegate tableView:tableView didSelectRowAtIndexPath:indexPath];
  397. }
  398.  
  399. if (self.expandOnlyOneCell)
  400. {
  401. if (selectedIndexPath)
  402. if (selectedIndexPath.row != - && selectedIndexPath.row != -) //collapse the last expanded item (if any)
  403. {
  404. BOOL dontExpandNewCell = NO;
  405. if (selectedIndexPath.row == indexPath.row && selectedIndexPath.section == indexPath.section)
  406. dontExpandNewCell = YES;
  407.  
  408. NSIndexPath* tmp = [NSIndexPath indexPathForRow:selectedIndexPath.row inSection:selectedIndexPath.section];//tmp now holds the last expanded item
  409. selectedIndexPath = [NSIndexPath indexPathForRow:- inSection:];
  410.  
  411. actionToTake = -;
  412.  
  413. [tableView beginUpdates];
  414. [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tmp] withRowAnimation:UITableViewRowAnimationAutomatic];
  415. [tableView endUpdates];
  416.  
  417. if (dontExpandNewCell) return; //the same expanded cell was touched and now I collapsed it. No new cell is touched
  418. }
  419.  
  420. actionToTake = ;
  421. ///expand the new touched item
  422.  
  423. selectedIndexPath = indexPath;
  424. [tableView beginUpdates];
  425. [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  426. [tableView endUpdates];
  427. if (self.enableAutoScroll)
  428. [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
  429.  
  430. }
  431. else
  432. {
  433. BOOL alreadyExpanded = NO;
  434. NSIndexPath* correspondingIndexPath;
  435. for (NSIndexPath* anIndexPath in expandedIndexPaths) {
  436. if (anIndexPath.row == indexPath.row && anIndexPath.section == indexPath.section)
  437. {alreadyExpanded = YES; correspondingIndexPath = anIndexPath;}
  438. }
  439.  
  440. if (alreadyExpanded)////collapse it!
  441. {
  442. actionToTake = -;
  443. [expandedIndexPaths removeObject:correspondingIndexPath];
  444. [tableView beginUpdates];
  445. [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  446. [tableView endUpdates];
  447. }
  448. else ///expand it!
  449. {
  450. actionToTake = ;
  451. [expandedIndexPaths addObject:indexPath];
  452. [tableView beginUpdates];
  453. // [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  454. [tableView endUpdates];
  455. if (self.enableAutoScroll)
  456. [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
  457. }
  458. }
  459. }
  460.  
  461. /*
  462. // Only override drawRect: if you perform custom drawing.
  463. // An empty implementation adversely affects performance during animation.
  464. - (void)drawRect:(CGRect)rect
  465. {
  466. // Drawing code
  467. }
  468. */
  469.  
  470. @end

针对源码修改过的地方:

使用HVTableView动态展开tableView中的cell的更多相关文章

  1. 动态切换tableView中的cell的种类

    动态切换tableView中的cell的种类 为什么要动态切换tableView中cell的种类呢?如果项目经理不出这种需求,你也就见不到这篇文章了:) 效果: 源码: 首先,你要准备3种cell,直 ...

  2. 动态展开tableView的cell[1]

    动态展开tableView的cell[1] 源码地址:https://github.com/xerxes235/HVTableView 虽然作者写的demo很好看,可是,你很难理解他是怎么玩的-_-! ...

  3. iOS中关于动态Tableview中的cell数据传输的多线程问题解决之拙见

    iOS中关于动态Tableview中的cell数据传输的多线程问题解决之拙见 (2015-12-05 12:48:20)[编辑][删除] 转载▼     首先我们先明确一下问题: 1.因为UI是在主线 ...

  4. 动态展开tableView的cell[2]

    动态展开tableView的cell[2] http://code4app.com/ios/%E5%8A%A8%E6%80%81%E6%B7%BB%E5%8A%A0cell/53845f8a933bf ...

  5. IOS中用UIFont返回字体的行高、动态改变tableView中Cell的高度

    一.动态改变Cell的高度 1.利用tableView代理方法的返回值决定每一行cell的高度 - (CGFloat)tableView:(UITableView *)tableView height ...

  6. 在tableView中设置cell的图片和文字

    // 设置UITableViewCellEditingStyle的 accessoryType UITableViewCellAccessoryNone,                   // d ...

  7. 关于TableView中出现deallocated问题

    Message sent to deallocated instance 关于的ios 开发中 deallocated问题,相信大家遇到了不少了: 关于怎么查找解决这个问题,特别是当问题在tableV ...

  8. 解决tableView中cell动态加载控件的重用问题

    解决tableView中cell动态加载控件的重用问题 tableView的cell,有时候需要在运行时取得对应的数据后才能够动态的创建该cell中的控件并加载到该cell中,此时,你一定会遇到重用问 ...

  9. Tableview中Dynamic Prototypes动态表的使用

    Tableview时IOS中应用非常广泛的控件,当需要动态的添加多条不同的数据时,需要用动态表来实现,下面给出一个小例子,适用于不确定Section的数目,并且每个Section中的行数也不同的情况, ...

随机推荐

  1. 装B 自卫神器 -

    IE内核下,使用XX插件 修改网页源代码直接无缝显示数据. 包括https FF同样. 截图: 可以修改所有客户端数据,用以迷惑他们.纯属娱乐 ``````` 再次,顺便提醒下. 做网赚的很多朋友,切 ...

  2. 创建自己的区块链游戏SLOT——以太坊代币(三)

    一个以太坊合约版本的轮盘游戏,向合约转账ETH,有几率获得3,5,10,100倍奖励 合约地址:0x53DA598E70a1505Ad95cBF17fc5DCA0d2c51174b 捐赠ETH地址:0 ...

  3. guava学习:guava集合类型-table

    最近学习了下guava的使用,这里简单记录下一些常用并且使用的工具类把. 看到table的使用时候真的是眼前一亮,之前的代码中写过很多的Map<String,Map<String,Stri ...

  4. WPF UpdateSourceTrigger属性

    TextBox中的变化并不是立即传递到源,而是在TextBox失去焦点后,源才更新.这种表现由绑定中的UpdateSourceTrigger属性来控制.它的默认值是Default,源会根据你绑定的属性 ...

  5. 09 jdk1.5的并发容器:CopyOnWriteArrayList(转载)

    原文链接:http://ifeve.com/java-copy-on-write/ Copy-On-Write简称COW,是一种用于程序设计中的优化策略. 其基本思路是,从一开始大家都在共享同一个内容 ...

  6. CentOS7手动修改系统时间

    CentOS7 永久修改系统时间 安装在虚拟机上的CentOS7的时间分为系统时间和硬件时间.二者都修改,重启系统(init 6 )才会永久生效.修改步骤如下 查看当前系统时间 date    修改当 ...

  7. java 初始化顺序问题

    今天在<thinking in java>上面看了关于初始化问题,之前从来都没有深入考虑过,这次算是把它搞明白了,所以记录一下: 这个不是我看到的初始化顺序问题,在网上搜索的时候发现的,感 ...

  8. 从零开始学JAVA(08)-使用SpringMVC4 Restful 风格引用静态文件 css/js/png

    在写完helloworld后想给网页加点样式(*.css),结果怎么也显示不了,百度了很多种方法后试行尝试,试验成功并记录下来,方便以后查看. 时隔两年,继续学习JAVA,太久没学了,忘记得差不多,还 ...

  9. IE浏览器上传文件后返回结果会自动弹出下载框

    服务器使用的是node,其它语言的后台没测试过. 在IE低版本浏览器下,当你上传一个文件后后台会返回一些数据,但是IE浏览器会弹出下载提示. 这个问题是之前处理的了,没有细究,今天有人问到这个问题,顺 ...

  10. String.IsNullOrEmpty 与 String.IsNullOrWhiteSpace

    String.IsNullOrEmpty 指示指定的字符串是否为 null 或者 空字符串: 返回值:如果参数为 null 或者 空字符串("" .String.Empty),结果 ...