近來公司的項目要寫個類似於QQ列表展開和收起的列表,想到以前李明傑視頻里說的,就寫個了展開和收起的Demo,希望給新手一點思路,與大家共同進步
我寫的這個展開和收起的思路是,用一個字典的Key去記錄點擊了第幾組,用狀態0-1去記錄他們是展開的還是收起的
廢話不多說,上代碼,注釋已寫,一定要認真看哦,認真看都能看懂
#import "ViewController.h" #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width @interface ViewController () <UITableViewDataSource,UITableViewDelegate> @property (nonatomic,strong)UITableView *tabelView; @property (nonatomic,strong)NSMutableArray *array; @property (nonatomic,strong)NSMutableDictionary *dict; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //創建tableView self.tabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain]; self.tabelView.delegate = self; self.tabelView.dataSource = self; [self.view addSubview:self.tabelView]; //記錄的字典 self.dict = [NSMutableDictionary dictionary]; //每組的標題 self.array = [[NSMutableArray alloc] init]; //去除tableView多余的橫線 self.tabelView.tableFooterView = [[UIView alloc] init]; for(int i=0;i<5;i++){ NSString *str = [NSString stringWithFormat:@"第%d組",i]; [self.array addObject:str]; } } //每組的組頭 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)]; view.backgroundColor = [UIColor cyanColor]; view.userInteractionEnabled = YES; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, (44-20)/2, 100, 20)]; label.text = self.array[section]; [view addSubview:label]; //view的tag就等於section 代表點擊了哪一個組 view.tag = section; [view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(display1:)]]; return view; } - (void)display1:(UITapGestureRecognizer *)g{ //將點擊了哪一組轉換成字符串 NSString *str = [NSString stringWithFormat:@"%ld",g.view.tag]; //從字典里面以第幾組為key取出狀態值 //如果狀態值為0,代表關閉 if([self.dict[str] integerValue] == 0){ [self.dict setObject:@(1) forKey:str]; } //如果狀態值為不為0,代表展開 else{ [self.dict setObject:@(0) forKey:str]; } //記得一定要刷新tabelView,不然沒有效果 [self.tabelView reloadData]; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 44; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 5; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { /*調用tableView的reloadData方法會重新調用這個方法 從而從字典里面取出相應組對應的狀態碼,從而判斷是需要展開還是收起 */ NSString *str = [NSString stringWithFormat:@"%ld",section]; //將點擊了哪一組轉換成字符串 if([self.dict[str] integerValue] == 1){ //如果狀態值為等於1,代表需要展開返回真正的多少個Cell return 5; }else{ //如果狀態值為等於0,代表需要收起返回0 return 0; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellId = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId]; if(cell == nil){ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; } cell.textLabel.text = @"每一行"; return cell; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
運行效果
我在公司項目做的效果,我感覺后面的箭頭還是做的不錯的,哈哈,大家可以下載我們應用看看雲收益Pro(iOS版本哦)
謝謝大家觀看