首页 > 代码库 > TableView下拉cell

TableView下拉cell


效果如下:

源码如下:

  1 //  2 //  MainViewController.m  3 //  NodeTableView  4 //  5 //  Created by ChenJungang on 14/11/10.  6 //  Copyright (c) 2014年 ChenJungang. All rights reserved.  7 //  8   9 #import "MainViewController.h" 10 #import "MainCell.h" 11  12 #define MAX_Count 55535 13  14  15 @interface MainViewController ()<UITableViewDataSource,UITableViewDelegate> 16  17 @property (strong, nonatomic) UITableView   *tableView; 18 @property (strong, nonatomic) NSArray       *dataArray; 19 @property (assign, nonatomic) NSInteger     rowCount; 20 @property (assign, nonatomic) NSInteger     sectionCount; 21  22 @end 23  24 @implementation MainViewController 25  26 - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ 27     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 28     if (self) { 29         // Custom initialization 30         self.rowCount = 0; 31         self.sectionCount = MAX_Count; 32     } 33     return self; 34 } 35  36 - (void)viewDidLoad { 37     [super viewDidLoad]; 38     self.title = @"node tableView"; 39     [self.view addSubview:self.tableView]; 40 } 41 -(NSArray*)dataArray { 42     if (!_dataArray) { 43         self.dataArray = @[@{@"array": @[@{@"name":@"11", @"position":@"position1"}, 44                                          @{@"name":@"12", @"position":@"position2"}, 45                                          @{@"name":@"13", @"position":@"position3"}, 46                                          @{@"name":@"14", @"position":@"position4"}, 47                                          @{@"name":@"15", @"position":@"position5"}],@"name":@"one", @"position":@"position—one"}, 48                            @{@"array": @[@{@"name":@"21", @"position":@"position1"}, 49                                          @{@"name":@"22", @"position":@"position2"}, 50                                          @{@"name":@"23", @"position":@"position3"}, 51                                          @{@"name":@"24", @"position":@"position4"}, 52                                          @{@"name":@"25", @"position":@"position5"}],@"name":@"two", @"position":@"position—two"}, 53                            @{@"array": @[@{@"name":@"31", @"position":@"position1"}, 54                                          @{@"name":@"32", @"position":@"position2"}, 55                                          @{@"name":@"33", @"position":@"position3"}, 56                                          @{@"name":@"34", @"position":@"position4"}, 57                                          @{@"name":@"35", @"position":@"position5"}],@"name":@"three", @"position":@"position—three"}, 58                            @{@"array": @[@{@"name":@"41", @"position":@"position1"}, 59                                          @{@"name":@"42", @"position":@"position2"}, 60                                          @{@"name":@"43", @"position":@"position3"}, 61                                          @{@"name":@"44", @"position":@"position4"}, 62                                          @{@"name":@"45", @"position":@"position5"}],@"name":@"four", @"position":@"position—four"}, 63                            @{@"array": @[@{@"name":@"51", @"position":@"position1"}, 64                                          @{@"name":@"52", @"position":@"position2"}, 65                                          @{@"name":@"53", @"position":@"position3"}, 66                                          @{@"name":@"54", @"position":@"position4"}, 67                                          @{@"name":@"55", @"position":@"position5"}],@"name":@"five", @"position":@"position—five"}]; 68     } 69     return _dataArray; 70 } 71 - (UITableView *)tableView{ 72     if (!_tableView) { 73         CGRect rect = self.view.bounds; 74         rect.origin.y = 0; 75         rect.size.height = rect.size.height - rect.origin.y; 76         _tableView = [[UITableView alloc]initWithFrame:rect style:UITableViewStylePlain]; 77         _tableView.delegate = self; 78         _tableView.dataSource = self; 79         _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; 80     } 81     return _tableView; 82 } 83  84 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 85     return self.dataArray.count; 86 } 87  88 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 89     if (_sectionCount != MAX_Count) { 90         if (section == _sectionCount) { 91             return 1 + _rowCount; 92         } 93         return 1; 94     }else{ 95         return 1; 96     } 97 } 98  99 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{100     static NSString *CellId = @"CellId";101     MainCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId];102     if (!cell) {103         cell = [MainCell loadFromXib];104     }105     if (indexPath.row == 0) {106         cell.nameLabel.text = self.dataArray[indexPath.section][@"name"];107         cell.positionLabel.text = self.dataArray[indexPath.section][@"position"];108     }else{109         cell.nameLabel.text = self.dataArray[indexPath.section][@"array"][indexPath.row-1][@"name"];110         cell.positionLabel.text = self.dataArray[indexPath.section][@"array"][indexPath.row-1][@"position"];111     }112     return cell;113 }114 115 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath116 {117     [tableView beginUpdates];118     [tableView deselectRowAtIndexPath:indexPath animated:YES];119     NSUInteger number =[self.dataArray[indexPath.section][@"array"]  count];120     if (indexPath.row == 0) {121         if(_sectionCount == MAX_Count){122             _sectionCount = indexPath.section;123             [self addCellwithNumber:number];124         }else if (_sectionCount == indexPath.section) {125             [self removeCellwithNumber:number];126             _sectionCount = MAX_Count;127         }else{128             [self removeCellwithNumber:number];129             _sectionCount = indexPath.section;130             [self addCellwithNumber:number];131         }132     }133     [tableView endUpdates];134 }135 136 -(void)removeCellwithNumber:(NSInteger)number137 {138     for (int i=(int)number; i>=1; i--) {139         --_rowCount;140         [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:i inSection:_sectionCount]]141                               withRowAnimation:UITableViewRowAnimationTop];142     }143     144 }145 -(void)addCellwithNumber:(NSInteger)number146 {147     for (int i=1; i<=number; i++) {148         ++_rowCount;149         [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:i inSection:_sectionCount]]150                               withRowAnimation:UITableViewRowAnimationTop];151     }152 }153 154 - (void)didReceiveMemoryWarning {155     [super didReceiveMemoryWarning];156     // Dispose of any resources that can be recreated.157 }158 159 @end
View Code

核心代码:

TableView下拉cell