用手风琴风格中的另一个自定义单元格Breplace现有的自​​定义单元格A – select

假设有不同高度的自定义单元A和B.
自定义单元格A在UITableView上加载默认值。 当用户select单元格A时 ,它将删除该单元格,并将该单元格B添加到该位置, 反之亦然 。 它将以手风琴风格进行重新resize的animation

要做到这一点,你应该有一个属性(或关键,如果使用一个字典)在你的数据数组来跟踪你想要在每个indexPath的细胞,并使用cellFroRowAtIndexPath中的if-else语句来使正确的单元格出队。 在didSelectRowAtIndexPath中,您将检查该属性,将其设置为相反,然后重新加载表。 您还需要实现heightForRowAtIndexPath,并检查相同的属性以确定要返回的高度。

编辑后:

如果你只需要跟踪一个选定的单元格,然后创build一个属性(我称之为selectedPath)来保存该值,并在heightForRowAtIndexPath和cellForRowAtIndexPath中检查它。 我在故事板中创build了两个单元格,一个是简单的UITableViewCell,另一个是RDCell类的自定义单元格。 我不知道这是否会给你想要的animation,但试试看看是否接近:

#import "TableController.h" #import "RDCell.h" @interface TableController () @property (strong,nonatomic) NSArray *theData; @property (nonatomic) NSIndexPath *selectedPath; @end @implementation TableController - (void)viewDidLoad { [super viewDidLoad]; self.theData = @[@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight"]; self.selectedPath = [NSIndexPath indexPathForRow:-1 inSection:0]; [self.tableView reloadData]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.theData.count; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.selectedPath isEqual:indexPath]) { return 90; }else{ return 44; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.selectedPath isEqual:indexPath]) { RDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RDCell" forIndexPath:indexPath]; cell.label.text = self.theData[indexPath.row]; return cell; }else{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; cell.textLabel.text = self.theData[indexPath.row]; return cell; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSIndexPath *oldPath = self.selectedPath; self.selectedPath = indexPath; [self.tableView reloadRowsAtIndexPaths:@[indexPath,oldPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } 
  **Got this amazing Solution, its working great...** @implementation NetworkCentreTable { NSMutableArray *arr; BOOL chk; int onSelectCount; NSIndexPath *onSelectTrack; } - (void)viewDidLoad { [super viewDidLoad]; arr=[[NSMutableArray alloc] initWithObjects:@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",nil]; onSelectCount=0; static NSString *CellIdentifier1 = @"NetworkCell2"; UINib *nib = [UINib nibWithNibName:@"NetworkCentreCellBig" bundle:nil]; [self.tblNetworkCentre registerNib:nib forCellReuseIdentifier:CellIdentifier1]; } #pragma mark - #pragma mark Custom Network TableView delegate and Datasource -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [arr count]; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier1 = @"NetworkCentreCell"; static NSString *CellIdentifier2 = @"NetworkCentreCellBig"; if(self.selectedRowIndex && indexPath.row == self.selectedRowIndex.integerValue) { NetworkCentreCell *cell = (NetworkCentreCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; UIViewController *controller=[[UIViewController alloc] initWithNibName:CellIdentifier2 bundle:nil]; cell=(NetworkCentreCell *)controller.view; return cell; } else { NetworkCentreCell *cell = (NetworkCentreCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; UIViewController *controller=[[UIViewController alloc] initWithNibName:CellIdentifier1 bundle:nil]; cell=(NetworkCentreCell *)controller.view; return cell; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { onSelectCount++; NSLog(@"num=%d",onSelectCount); self.selectedIndexPath = indexPath; [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationNone]; self.selectedRowIndex = [NSNumber numberWithInteger:indexPath.row]; [self.tblNetworkCentre deselectRowAtIndexPath:indexPath animated:YES]; //First we check if a cell is already expanded. //If it is we want to minimize make sure it is reloaded to minimize it back if( onSelectCount==1 ) { [tableView beginUpdates]; NSIndexPath *previousPath = [NSIndexPath indexPathForRow:self.selectedRowIndex.integerValue inSection:0]; self.selectedRowIndex = [NSNumber numberWithInteger:indexPath.row]; onSelectTrack=indexPath; [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView endUpdates]; } if(onSelectTrack.row!=indexPath.row) { [tableView beginUpdates]; [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:onSelectTrack] withRowAnimation:UITableViewRowAnimationFade]; [tableView endUpdates]; onSelectTrack=indexPath; onSelectCount=0; [self tableView:tableView didSelectRowAtIndexPath:onSelectTrack]; } if(self.selectedRowIndex.integerValue == indexPath.row && onSelectCount==2) { [tableView beginUpdates]; self.selectedRowIndex = [NSNumber numberWithInteger:-1]; [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; onSelectCount=0; [tableView endUpdates]; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if(self.selectedRowIndex && indexPath.row == self.selectedRowIndex.integerValue) { return 280; }else{ return 85; } } 

您可以在didSelectRowAtIndexPath方法中调用[yourTableview reloadData]。 然后在numberOfRowsInSection给出一个新的计数。 在heightForRowAtIndexpath中指定自定义高度。 在cellForRowAtIndexpath中添加自定义单元格。