将现有的自定义单元格A替换为手风琴样式中的另一个自定义单元格B - 选择时

Viz*_*llx -1 objective-c uitableview ipad ios

假设有自定单元A&不同高度的乙.
自定义单元格A在UITableView上加载默认值.当用户选择单元格A时,它将删除该单元格并将单元格B添加到该位置,反之亦然.它将以手风琴风格进行重新调整大小的动画.

rde*_*mar 5

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

编辑后:

如果您只需要跟踪一个选定的单元格,那么创建一个属性(我称之为selectedPath)来保存该值并在heightForRowAtIndexPath和cellForRowAtIndexPath中进行检查.我在故事板中创建了两个单元格,一个是简单的UITableViewCell,另一个是RDCell类的自定义单元格.我不确定这是否会给你想要的动画,但是试一试,看看它是否接近:

#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];
}
Run Code Online (Sandbox Code Playgroud)