- (BOOL)cellIsSelected:(NSIndexPath *)indexPath
{
// Return whether the cell at the specified index path is selected or not
NSNumber *selectedIndex = [selectedIndexes objectForKey:indexPath];
return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Deselect cell
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Toggle 'selected' state
BOOL isSelected = ![self cellIsSelected:indexPath];
// Store cell 'selected' state keyed on indexPath
NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
[selectedIndexes setObject:selectedIndex forKey:indexPath];
// This is where magic happens...
[beveragesTableView beginUpdates];
[beveragesTableView endUpdates];
[selectedIndexes removeAllObjects];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// If our cell is selected, return double height
if ([self cellIsSelected:indexPath] && ![[descriptionTypeArray objectAtIndex:indexPath.row] isEqualToString:@" "])
{
return kCellHeight * 2.0;
}
// Cell isn't selected so return single height
return kCellHeight;
}
selectedIndexes is an object ofNSMutableDictonary which is declared in .h file
i have created tableview where it animates based upon description present in the cell or not .. while it animates , if i select a row the row size increases and when i tap on it once again the row size should go back to normal height , i am only able to do that when i select another row/cell , i want to cell to go back to normal height when i tap on the selected row .
This code was just tested and it works. You will have to modify it for your needs but the basic behaviour should suit your needs.
Note that there is no need to call beginUpdates/endUpdates
.
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSIndexPath *lastSelectedCell;;
@end
ViewController.m
#import "ViewController.h"
@implementation ViewController
@synthesize tableView = _tableView;
@synthesize lastSelectedCell = _lastSelectedCell;;
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIdentifier"];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = [NSString stringWithFormat:@"row %d %@",indexPath.row, ([indexPath compare:_lastSelectedCell] == NSOrderedSame)?@"S":@"-"];
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"S: %d", indexPath.row);
if (([indexPath compare:_lastSelectedCell] == NSOrderedSame)) {
_lastSelectedCell = nil;
} else {
[self setLastSelectedCell: indexPath];
}
[tableView reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return ([indexPath compare:_lastSelectedCell] == NSOrderedSame)?80.0:40.0;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
_tableView.dataSource = self;
_tableView.delegate = self;
}
- (void)viewDidUnload
{
[super viewDidUnload];
_tableView = nil;
_lastSelectedCell = nil;
}
@end