使用HVTableView动态展开tableView中的cell

效果:

源码:

HVTableView.h 与 HVTableView.m

//
// HVTableView.h
// HRVTableView
//
// Created by Hamidreza Vakilian on 25/11/2013
// Copyright (c) 2013 Hamidreza Vakilian. All rights reserved.
// Website: http://www.infracyber.com/
// Email: xerxes235@yahoo.com
// //
//*************** HVTableView - UITableView with expand/collapse feature *****************************
// by hamidreza vakilian
//
//
// This is a subclass of UITableView with expand/collapse feature that may come so handy in many apps.
// The developer can save a lot of time using an expand/collapse tableView instead of creating a detail viewController for
// every cell. I mean the details of each cell can be displayed immediately on the same table without switching to
// another view. On the other hand in my opinion it far more impressive and eye-catching from a regular user's view.
//
// To create an instance of HVTableView you go by code.
// (currently I you can't do it from xib - I will work on that later)
// That's simple as:
//
// HVTableView* myTable = [[HVTableView alloc] initWithFrame:CGRectMake(84, 250, 600, 600) expandOnlyOneCell:NO enableAutoScroll:YES];
// myTable.HVTableViewDelegate = self;
// myTable.HVTableViewDataSource = self;
// [myTable reloadData];
// [self.view addSubview:myTable]; // Two important parameters when initializing the HVTableView
// if expandOnlyOneCell==TRUE: Just one cell will be expanded at a time.
// if expandOnlyOneCell==FALSE: multiple cells can be expanded at a time
//
// if enableAutoScroll==TRUE: when the user touches a cell, the HVTableView will automatically scroll to it
// 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
//
//
// Your viewController must conform to HVTableViewDelegate, HVTableViewDataSource protocols. Just like the regular
// UITableView
//
// Like before you implement these familiar delegate functions:
// -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
// -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//
// I added a boolean parameter the heightForRowAtIndexPath function so you will return different values for an expanded or a collapsed cell.
// (isExpanded==TRUE: return the size of the cell in expanded state)
// (isExpanded==FALSE: return the size of the cell in collapsed (initial) state)
//
// -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath isExpanded:(BOOL)isexpanded
//
//
// I also added a boolean parameter to the cellForRowAtIndexPath function too. update the cell's content respecting it's state (isExpanded)
// -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath isExpanded:(BOOL)isExpanded
//
//
// 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:
// -(void)tableView:(UITableView *)tableView collapseCell: (UITableViewCell*)cell withIndexPath:(NSIndexPath*) indexPath;
//
// 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:
// -(void)tableView:(UITableView *)tableView expandCell: (UITableViewCell*)cell withIndexPath:(NSIndexPath*) indexPath;
//
// 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.
//
//
// 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.
//
//
// Please don't hesitate to mail me your feedbacks and suggestions or a bug report. I would be very thankful of your responses.
//
// 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:
// 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
//
//
// Thanks,....
// *************************************************************************************************************** #import <UIKit/UIKit.h> @protocol HVTableViewDataSource <NSObject>
@required
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath isExpanded:(BOOL)isExpanded;
-(void)tableView:(UITableView *)tableView collapseCell: (UITableViewCell*)cell withIndexPath:(NSIndexPath*) indexPath;
-(void)tableView:(UITableView *)tableView expandCell: (UITableViewCell*)cell withIndexPath:(NSIndexPath*) indexPath;
@optional - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
@end @protocol HVTableViewDelegate <NSObject>
@optional
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath isExpanded:(BOOL)isExpanded;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0);
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath NS_DEPRECATED_IOS(2_0, 3_0);
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath;
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0);
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0);
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0);
@end @interface HVTableView : UITableView <UITableViewDataSource, UITableViewDelegate>
{ NSIndexPath *selectedIndexPath;
int actionToTake;
NSMutableArray* expandedIndexPaths;
} @property (weak,nonatomic) id <HVTableViewDelegate> HVTableViewDelegate;
@property (weak,nonatomic) id <HVTableViewDataSource> HVTableViewDataSource;
@property (nonatomic) BOOL expandOnlyOneCell;
@property (nonatomic) BOOL enableAutoScroll; - (id)initWithFrame:(CGRect)frame
expandOnlyOneCell:(BOOL)expandOnlyOneCell
enableAutoScroll:(BOOL)enableAutoScroll; @end
//
// HVTableView.m
// HRVTableView
//
// Created by Hamidreza Vakilian on 25/11/2013
// Copyright (c) 2013 Hamidreza Vakilian. All rights reserved.
// Website: http://www.infracyber.com/
// Email: xerxes235@yahoo.com
// #import "HVTableView.h" @implementation HVTableView
@synthesize HVTableViewDelegate, HVTableViewDataSource; - (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if(self) {
self.delegate = self;
self.dataSource = self;
expandedIndexPaths = [NSMutableArray new];
}
return self;
} - (id)initWithFrame:(CGRect)frame expandOnlyOneCell:(BOOL)expandOnlyOneCell enableAutoScroll: (BOOL)enableAutoScroll
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_expandOnlyOneCell = expandOnlyOneCell;
if (!_expandOnlyOneCell)
expandedIndexPaths = [[NSMutableArray alloc] init]; _enableAutoScroll = enableAutoScroll; self.delegate = self;
self.dataSource = self; }
return self;
} //////// IMPORTANT!!!!!!!!!!!!!!!!!!!!!
//////// UITableViewDataSource Protocol Forwarding
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [HVTableViewDataSource tableView:tableView numberOfRowsInSection:section];
} -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [HVTableViewDataSource numberOfSectionsInTableView:tableView];
} - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if ([HVTableViewDataSource respondsToSelector:@selector(tableView:titleForHeaderInSection:)])
return [HVTableViewDataSource tableView:tableView titleForHeaderInSection:section];
return nil;
} - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{ if ([HVTableViewDataSource respondsToSelector:@selector(tableView:titleForFooterInSection:)])
return [HVTableViewDataSource tableView:tableView titleForFooterInSection:section];
return nil;
} - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{ if ([HVTableViewDataSource respondsToSelector:@selector(tableView:canEditRowAtIndexPath:)])
return [HVTableViewDataSource tableView:tableView canEditRowAtIndexPath:indexPath];
return NO;
} - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([HVTableViewDataSource respondsToSelector:@selector(tableView:canMoveRowAtIndexPath:)])
return [HVTableViewDataSource tableView:tableView canMoveRowAtIndexPath:indexPath];
return NO;
} - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
if ([HVTableViewDataSource respondsToSelector:@selector(sectionIndexTitlesForTableView:)])
return [HVTableViewDataSource sectionIndexTitlesForTableView:tableView];
return nil;
} - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
if ([HVTableViewDataSource respondsToSelector:@selector(tableView:sectionForSectionIndexTitle:atIndex:)])
return [HVTableViewDataSource tableView:tableView sectionForSectionIndexTitle:title atIndex:index];
return ;
} - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([HVTableViewDataSource respondsToSelector:@selector(tableView:commitEditingStyle:forRowAtIndexPath:)])
return [HVTableViewDataSource tableView:tableView commitEditingStyle:editingStyle forRowAtIndexPath:indexPath];
} - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
if ([HVTableViewDataSource respondsToSelector:@selector(tableView:moveRowAtIndexPath:toIndexPath:)])
return [HVTableViewDataSource tableView:tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
} -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell; if (self.expandOnlyOneCell)
{
if (actionToTake == ) // e.g. the first time or an expanded cell from before gets in to view
{
if (selectedIndexPath)
if (selectedIndexPath.row == indexPath.row && selectedIndexPath.section == indexPath.section)
{
cell = [HVTableViewDataSource tableView:tableView cellForRowAtIndexPath:indexPath isExpanded:YES];//i want it expanded
return cell;
} cell = [HVTableViewDataSource tableView:tableView cellForRowAtIndexPath:indexPath isExpanded:NO]; return cell; //it's already collapsed!
} cell = [HVTableViewDataSource tableView:tableView cellForRowAtIndexPath:indexPath isExpanded:NO]; if(actionToTake == -)
{
[HVTableViewDataSource tableView:tableView collapseCell:cell withIndexPath:indexPath];
actionToTake = ;
}
else
{
[HVTableViewDataSource tableView:tableView expandCell:cell withIndexPath:indexPath];
actionToTake = ;
}
}
else
{
if (actionToTake == ) // e.g. the first time or an expanded cell from before gets in to view
{
BOOL alreadyExpanded = NO;
NSIndexPath* correspondingIndexPath;
for (NSIndexPath* anIndexPath in expandedIndexPaths) {
if (anIndexPath.row == indexPath.row && anIndexPath.section == indexPath.section)
{alreadyExpanded = YES; correspondingIndexPath = anIndexPath;}
} if (alreadyExpanded)
cell = [HVTableViewDataSource tableView:tableView cellForRowAtIndexPath:indexPath isExpanded:YES];
else
cell = [HVTableViewDataSource tableView:tableView cellForRowAtIndexPath:indexPath isExpanded:NO]; return cell; //it's already collapsed! } cell = [HVTableViewDataSource tableView:tableView cellForRowAtIndexPath:indexPath isExpanded:NO]; if(actionToTake == -)
{
[HVTableViewDataSource tableView:tableView collapseCell:cell withIndexPath:indexPath];
actionToTake = ;
}
else
{
[HVTableViewDataSource tableView:tableView expandCell:cell withIndexPath:indexPath];
actionToTake = ;
}
} return cell;
} //////// IMPORTANT!!!!!!!!!!!!!!!!!!!!!
//////// IMPORTANT!!!!!!!!!!!!!!!!!!!!!
//////// IMPORTANT!!!!!!!!!!!!!!!!!!!!!
//////// IMPORTANT!!!!!!!!!!!!!!!!!!!!!
//////// IMPORTANT!!!!!!!!!!!!!!!!!!!!!
//////// UITableViewDelegate Protocol Forwarding
//////// NOTE::::: If you want to use any of these protocols in your code; you must uncomment it. I have comment them for performance reasons
/*
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:willDisplayCell:forRowAtIndexPath:)])
[HVTableViewDelegate tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
} -(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:willDisplayHeaderView:forSection:)])
[HVTableViewDelegate tableView:tableView willDisplayHeaderView:view forSection:section];
} -(void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:willDisplayFooterView:forSection:)])
[HVTableViewDelegate tableView:tableView willDisplayFooterView:view forSection:section];
} -(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:didEndDisplayingCell:forRowAtIndexPath:)])
[HVTableViewDelegate tableView:tableView didEndDisplayingCell:cell forRowAtIndexPath:indexPath];
} - (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:didEndDisplayingHeaderView:forSection:)])
[HVTableViewDelegate tableView:tableView didEndDisplayingHeaderView:view forSection:section];
} - (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:didEndDisplayingFooterView:forSection:)])
[HVTableViewDelegate tableView:tableView didEndDisplayingFooterView:view forSection:section];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:heightForHeaderInSection:)])
return [HVTableViewDelegate tableView:tableView heightForHeaderInSection:section];
return 0;
} - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:heightForFooterInSection:)])
return [HVTableViewDelegate tableView:tableView heightForFooterInSection:section];
return 0;
} - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0)
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:estimatedHeightForRowAtIndexPath:)])
return [HVTableViewDelegate tableView:tableView estimatedHeightForRowAtIndexPath:indexPath];
return 100;
} - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0)
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:estimatedHeightForHeaderInSection:)])
return [HVTableViewDelegate tableView:tableView estimatedHeightForHeaderInSection:section];
return 0;
} - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:estimatedHeightForFooterInSection:)])
return [HVTableViewDelegate tableView:tableView estimatedHeightForFooterInSection:section];
return 0;
} - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:viewForHeaderInSection:)])
return [HVTableViewDelegate tableView:tableView viewForHeaderInSection:section];
return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:viewForFooterInSection:)])
return [HVTableViewDelegate tableView:tableView viewForFooterInSection:section];
return nil;
} - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:accessoryButtonTappedForRowWithIndexPath:)])
[HVTableViewDelegate tableView:tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
} - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:shouldHighlightRowAtIndexPath:)])
return [HVTableViewDelegate tableView:tableView shouldHighlightRowAtIndexPath:indexPath];
return YES;
}
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:didHighlightRowAtIndexPath:)])
[HVTableViewDelegate tableView:tableView didHighlightRowAtIndexPath:indexPath];
}
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:didUnhighlightRowAtIndexPath:)])
[HVTableViewDelegate tableView:tableView didUnhighlightRowAtIndexPath:indexPath];
}
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:willDeselectRowAtIndexPath:)])
return [HVTableViewDelegate tableView:tableView willDeselectRowAtIndexPath:indexPath];
return nil;
} - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:didDeselectRowAtIndexPath:)])
[HVTableViewDelegate tableView:tableView didDeselectRowAtIndexPath:indexPath];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:editingStyleForRowAtIndexPath:)])
return [HVTableViewDelegate tableView:tableView editingStyleForRowAtIndexPath:indexPath];
return UITableViewCellEditingStyleNone;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:)])
return [HVTableViewDelegate tableView:tableView titleForDeleteConfirmationButtonForRowAtIndexPath:indexPath];
return nil;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:shouldIndentWhileEditingRowAtIndexPath:)])
return [HVTableViewDelegate tableView:tableView shouldIndentWhileEditingRowAtIndexPath:indexPath];
return NO;
}
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:willBeginEditingRowAtIndexPath:)])
[HVTableViewDelegate tableView:tableView willBeginEditingRowAtIndexPath:indexPath];
}
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:didEndEditingRowAtIndexPath:)])
[HVTableViewDelegate tableView:tableView didEndEditingRowAtIndexPath:indexPath];
}
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:)])
return [HVTableViewDelegate tableView:tableView targetIndexPathForMoveFromRowAtIndexPath:sourceIndexPath toProposedIndexPath:proposedDestinationIndexPath];
return nil;
}
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:indentationLevelForRowAtIndexPath:)])
return [HVTableViewDelegate tableView:tableView indentationLevelForRowAtIndexPath:indexPath];
return 0;
}
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:shouldShowMenuForRowAtIndexPath:)])
return [HVTableViewDelegate tableView:tableView shouldShowMenuForRowAtIndexPath:indexPath];
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:canPerformAction:forRowAtIndexPath:withSender:)])
return [HVTableViewDelegate tableView:tableView canPerformAction:action forRowAtIndexPath:indexPath withSender:sender];
return NO;
}
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
if ([HVTableViewDelegate respondsToSelector:@selector(tableView:performAction:forRowAtIndexPath:withSender:)])
return [HVTableViewDelegate tableView:tableView performAction:action forRowAtIndexPath:indexPath withSender:sender];
}
*/ -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.expandOnlyOneCell)
{
if (selectedIndexPath)
if(selectedIndexPath.row == indexPath.row && selectedIndexPath.section == indexPath.section)
return [HVTableViewDelegate tableView:tableView heightForRowAtIndexPath:indexPath isExpanded:YES]; return [HVTableViewDelegate tableView:tableView heightForRowAtIndexPath:indexPath isExpanded:NO];
}
else
{
BOOL alreadyExpanded = NO;
NSIndexPath* correspondingIndexPath;
for (NSIndexPath* anIndexPath in expandedIndexPaths) {
if (anIndexPath.row == indexPath.row && anIndexPath.section == indexPath.section)
{alreadyExpanded = YES; correspondingIndexPath = anIndexPath;}
}
if (alreadyExpanded)
return [HVTableViewDelegate tableView:tableView heightForRowAtIndexPath:indexPath isExpanded:YES];
else
return [HVTableViewDelegate tableView:tableView heightForRowAtIndexPath:indexPath isExpanded:NO];
}
} -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath;
} -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 更新代理,让外面的tableView也可以使用
if (self.HVTableViewDelegate && [self.HVTableViewDelegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
[self.HVTableViewDelegate tableView:tableView didSelectRowAtIndexPath:indexPath];
} if (self.expandOnlyOneCell)
{
if (selectedIndexPath)
if (selectedIndexPath.row != - && selectedIndexPath.row != -) //collapse the last expanded item (if any)
{
BOOL dontExpandNewCell = NO;
if (selectedIndexPath.row == indexPath.row && selectedIndexPath.section == indexPath.section)
dontExpandNewCell = YES; NSIndexPath* tmp = [NSIndexPath indexPathForRow:selectedIndexPath.row inSection:selectedIndexPath.section];//tmp now holds the last expanded item
selectedIndexPath = [NSIndexPath indexPathForRow:- inSection:]; actionToTake = -; [tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tmp] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates]; if (dontExpandNewCell) return; //the same expanded cell was touched and now I collapsed it. No new cell is touched
} actionToTake = ;
///expand the new touched item selectedIndexPath = indexPath;
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
if (self.enableAutoScroll)
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; }
else
{
BOOL alreadyExpanded = NO;
NSIndexPath* correspondingIndexPath;
for (NSIndexPath* anIndexPath in expandedIndexPaths) {
if (anIndexPath.row == indexPath.row && anIndexPath.section == indexPath.section)
{alreadyExpanded = YES; correspondingIndexPath = anIndexPath;}
} if (alreadyExpanded)////collapse it!
{
actionToTake = -;
[expandedIndexPaths removeObject:correspondingIndexPath];
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
}
else ///expand it!
{
actionToTake = ;
[expandedIndexPaths addObject:indexPath];
[tableView beginUpdates];
// [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
if (self.enableAutoScroll)
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
}
} /*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/ @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. Hive的Shell里hive> 执行操作时,出现FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask错误的解决办法(图文详解)

    不多说,直接上干货! 这个问题,得非 你的hive和hbase是不是同样都是CDH版本,还是一个是apache版本,一个是CDH版本. 问题详情 [kfk@bigdata-pro01 apache-h ...

  2. QQ空间首页背景图片淡出解析与不足完善

    一件事情的发生总是有原因的,当然更多的是对技术本身的追求,一定要搞懂啦,废话不多说,大宝剑直插主题. 起因 以前做过一个xx项目,在登陆界面背景图片中,直接引用了一张大图,css类似于这样(backg ...

  3. Struts dispatchAction

    在Struts中定义动态Action,不用定义多个Action,可以实现一个action,多个跳转. 在定义时,继承DispatchAction,并定义parameter的名字 在jsp页面选择act ...

  4. R语言变量赋值

    变量可以使用向左,向右且等于操作符来分配值.可以使用 print() 或 cat() 函数打印变量的值.cat() 函数将多个项目并成连续并打印输出. # Assignment using equal ...

  5. 百度优先收录HTTPS网站?你的网站https还在等什么

    2015年5月25日,百度站长平台发布的公告,称将正式开放对HTTPS站点的收录.开始优先抓取HTTPS站点.所有事情都有两面性,这个消息对于已 经到HTTPS的网站来说是个喜大普奔的好消息.对于需要 ...

  6. ios碎片小记

    一.UIImageView 1.图片形状设为圆形时可能会由于图片的宽高比例导致显示出来的效果不是圆形 解决:设置UIImageView的contentMode为UIViewContentModeSca ...

  7. 【总】java面试题

    100+经典Java面试题及答案解析 https://www.cnblogs.com/pureEve/p/6546280.html 2017 最新java面试题(技术面试) https://blog. ...

  8. .NET中的集合-ArrayList2

    数组特点:类型同统一,长度固定 集合常用操作 添加 遍历 移除 ArrayList可变长度数组,使用类似于数组 属性:capacity(集合中可以容纳元素的个数,翻倍增长)count(集合中实际存放的 ...

  9. 浏览器获取正确的scrollTop值

    window.pageYOffset 被所有浏览器支持除了 IE 6, IE 7, IE 8, 不关doctype的事, 注IE9 开始支持此属性. window.scrollY 被Firefox, ...

  10. PhpStorm 破解及 XDebug 调试

    PhpStorm 破解及 XDebug 调试 PhpStorm 破解 PhpStorm 10.0.2 破解 地址:http://jingyan.baidu.com/article/20095761cb ...