Cell Expansion Methods: cell expansion methods
1. Insert a new cell
Principle:
(1) define whether to expand and the subscript of the expanded cell
@ Property (assign, nonatomic) BOOL isExpand; // whether to expand @ property (strong, nonatomic) NSIndexPath * selectedIndexPath; // subscript of the expanded cell
(2) create two different cells
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell; if (self.isExpand && self.selectedIndexPath.row < indexPath.row && indexPath.row <= self.selectedIndexPath.row + ExpandCount) { // Expand cell cell = [tableView dequeueReusableCellWithIdentifier:@"CsutomExpansionCell" forIndexPath:indexPath]; } else { // Normal cell cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath]; } return cell;}
(3) create the number of cells you need
if (self.isExpand) { return CellCount + ExpandCount; } return CellCount;
(4) Insert the cells you want to display under the cell you click (multiple cells can be expanded) and click Delete again.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (!self.selectedIndexPath) { self.isExpand = YES; self.selectedIndexPath = indexPath; [self.tavleView beginUpdates]; [self.tavleView insertRowsAtIndexPaths:[self indexPathsForExpandRow:indexPath.row] withRowAnimation:UITableViewRowAnimationTop]; [self.tavleView endUpdates]; } else { if (self.isExpand) { if (self.selectedIndexPath == indexPath) { self.isExpand = NO; [self.tavleView beginUpdates]; [self.tavleView deleteRowsAtIndexPaths:[self indexPathsForExpandRow:indexPath.row] withRowAnimation:UITableViewRowAnimationTop]; [self.tavleView endUpdates]; self.selectedIndexPath = nil; } else if (self.selectedIndexPath.row < indexPath.row && indexPath.row <= self.selectedIndexPath.row + ExpandCount) { } else { self.isExpand = NO; [self.tavleView beginUpdates]; [self.tavleView deleteRowsAtIndexPaths:[self indexPathsForExpandRow:self.selectedIndexPath.row] withRowAnimation:UITableViewRowAnimationTop]; [self.tavleView endUpdates]; self.selectedIndexPath = nil; } } }}#pragma mark - other- (NSArray *)indexPathsForExpandRow:(NSInteger)row { NSMutableArray *indexPaths = [NSMutableArray array]; for (int i = 1; i <= ExpandCount; i++) { NSIndexPath *idxPth = [NSIndexPath indexPathForRow:row + i inSection:0]; [indexPaths addObject:idxPth]; } return [indexPaths copy];}
2. Insert cells in different sections
Principle:
(1) define whether to expand and the subscript of the expanded cell
(2) create two different cells
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell; if (self.isExpand && self.selectedIndexPath.section == indexPath.section) { // Expand Cell cell = [tableView dequeueReusableCellWithIdentifier:@"CsutomExpansionCell" forIndexPath:indexPath]; } else { // Normal Cell cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath]; } return cell;}
(3) create the number of cells and sections in normal state.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return SectionCount;}
(4) change the number of cells in the expanded section when you expand it.
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {if (self. isExpand & self. selectedIndexPath. section = section) {return 1 + ExpandCount; // multiple quantity} return 1 ;}
(5) Insert the cell you want to display into the section of the cell you click (multiple cells can be expanded) and click Delete again.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (!self.selectedIndexPath) { self.isExpand = YES; self.selectedIndexPath = indexPath; [self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths:[self indexPathsForExpandSection:indexPath.section] withRowAnimation:UITableViewRowAnimationTop]; [self.tableView endUpdates]; } else { if (self.isExpand) { if (self.selectedIndexPath == indexPath) { self.isExpand = NO; [self.tableView beginUpdates]; [self.tableView deleteRowsAtIndexPaths:[self indexPathsForExpandSection:indexPath.section] withRowAnimation:UITableViewRowAnimationTop]; [self.tableView endUpdates]; self.selectedIndexPath = nil; } else if (self.selectedIndexPath.row != indexPath.row && indexPath.section <= self.selectedIndexPath.section) { // Select the expand cell, do the relating dealing. } else { self.isExpand = NO; [self.tableView beginUpdates]; [self.tableView deleteRowsAtIndexPaths:[self indexPathsForExpandSection:self.selectedIndexPath.section] withRowAnimation:UITableViewRowAnimationTop]; [self.tableView endUpdates]; self.selectedIndexPath = nil; } } }}- (NSArray *)indexPathsForExpandSection:(NSInteger)section { NSMutableArray *indexPaths = [NSMutableArray array]; for (int i = 1; i <= ExpandCount; i++) { NSIndexPath *idxPth = [NSIndexPath indexPathForRow:i inSection:section]; [indexPaths addObject:idxPth]; } return [indexPaths copy];}
3. Change the cell height
Principle:
(1) define whether to expand and the subscript of the expanded cell
(2) create a cell, which is divided into the upper half and the lower half.
(3) the height of the cell to be created, which is divided into the height under normal circumstances and the height after expansion
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (self.isExpand && self.selectedIndexPath == indexPath) { return 121; } else { return 44; }}
(4) Click to refresh the clicked cell to the clicked cell.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (!self.selectedIndexPath) { self.isExpand = YES; self.selectedIndexPath = indexPath; [self.tableView beginUpdates]; [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [self.tableView endUpdates]; } else { if (self.isExpand) { if (self.selectedIndexPath == indexPath) { self.isExpand = NO; [self.tableView beginUpdates]; [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [self.tableView endUpdates]; self.selectedIndexPath = nil; } else { self.isExpand = NO; [self.tableView beginUpdates]; [self.tableView reloadRowsAtIndexPaths:@[self.selectedIndexPath] withRowAnimation:UITableViewRowAnimationFade]; [self.tableView endUpdates]; self.selectedIndexPath = nil; } } }}
4. Customize the section and click to expand the corresponding cell (there is no time to write ...)
Demo Link
Http://pan.baidu.com/s/1c0YQDNE