cell展開的幾種方式,cell展開幾種方式

來源:互聯網
上載者:User

cell展開的幾種方式,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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.