• cell展开的几种方式


    一.插入新的cell

    原理:

    (1)定义是否展开,和展开的cell的下标

    @property (assign, nonatomic) BOOL isExpand; //是否展开
    @property (strong, nonatomic) NSIndexPath *selectedIndexPath;//展开的cell的下标

    (2)创建两个不同的cell

     

    - (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)创建你需要的cell的数量

     if (self.isExpand) {
            return CellCount + ExpandCount;
        }
        return CellCount;

    (4)点击的时候向点击的cell下面插入你需要展示的cell(可展开多个),再次点击删除

    - (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];
    }

    二.在不同的section里插入cell

    原理:

    (1)定义是否展开,和展开的cell的下标

    (2)创建两个不同的cell

    - (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)创建你需要展示普通状态下cell,section的数量

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return SectionCount;
    }

    (4)改变你展开的时候,展开的section的cell的数量

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if (self.isExpand && self.selectedIndexPath.section == section) {
            return 1 + ExpandCount; //多个数量
        }
        return 1;
    }

    (5)点击的时候向点击的cell的section内插入你需要展示的cell(可展开多个),再次点击删除

    - (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];
    }

    三.更改cell的高度

    原理:

    (1)定义是否展开,和展开的cell的下标

    (2)创建一个的cell,分上半部分和下半部分

    (3)创建cell的高度,分普通情况下的高度和展开后的高度

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (self.isExpand && self.selectedIndexPath == indexPath) {
            return 121;
        } else {
            return 44;
        }
    }

    (4)点击的时候向点击的cell刷新点击的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;
                }
            }
        }
    }

    四.自定义section,点击展开相应的cell(下午有空写...)

    demo链接

    http://pan.baidu.com/s/1c0YQDNE

    效果图

  • 相关阅读:
    C++中四大强制类型转换!
    队列(queue)的实现
    栈(stack)的实现
    单向链表
    十种排序算法详解及C++实现
    extern “C”
    C语言内存分配及各种数据存储位置
    Python中的classmethod与staticmethod
    关于ORM,以及Python中SQLAlchemy的sessionmaker,scoped_session
    Python中的SQLAlchemy
  • 原文地址:https://www.cnblogs.com/hxwj/p/5035506.html
Copyright © 2020-2023  润新知