TableView 点击cell,改变选中cell的高度【转】
2017-02-19 本文已影响84人
FANTASIED
NSMutableDictionary *selectedIndexes;
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath;
- (void)viewDidLoad {
[super viewDidLoad];
selectedIndexes = [[NSMutableDictionary alloc]init];
}
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
// Return whether the cell at the specified index path is selected or not
NSNumber *selectedIndex = [selectedIndexesobjectForKey:indexPath];
return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}
//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//return 35;
if ([self cellIsSelected:indexPath]) {
return 60;
}
return 35;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Deselect cell
[tableView deselectRowAtIndexPath:indexPathanimated:TRUE];
// Toggle 'selected' state
BOOL isSelected = ![selfcellIsSelected:indexPath];
// Store cell 'selected' state keyed on indexPath
NSNumber *selectedIndex = [NSNumbernumberWithBool:isSelected];
[selectedIndexes setObject:selectedIndex forKey:indexPath];
// This is where magic happens...
[self.tableView beginUpdates];
[self.tableView endUpdates];
}