iOS---》点击uitableview 的section展开或隐藏
#import <UIKit/UIKit.h> @interface TestCell : UITableViewCell @property (weak, nonatomic) IBOutlet UILabel *firstLabel; @property (weak, nonatomic) IBOutlet UILabel *endLabel; @property (weak, nonatomic) IBOutlet UIView *myView; @end #import "TestCell.h" @implementation TestCell - (void)awakeFromNib { _myView.layer.borderColor=[UIColor clearColor].CGColor; // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end #import "ViewController.h" #import "TestCell.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> { NSMutableArray *rowArray; BOOL *flag; } @property (weak, nonatomic) IBOutlet UITableView *myTableView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; rowArray=[NSMutableArray arrayWithObjects:@"1",@"2",@"3", @"4",@"5",@"6",@"7",@"8",@"9",nil]; flag = (BOOL*)malloc(rowArray.count*sizeof(BOOL*)); memset(flag, NO, sizeof(*flag)); //_myTableView.separatorStyle=UITableViewCellSeparatorStyleNone; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return rowArray.count; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (flag[section]) { return rowArray.count; } else { return 0; } } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TestCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TestCell"]; cell.firstLabel.text=rowArray[indexPath.row]; cell.endLabel.text=rowArray[indexPath.row]; return cell; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (section==0) { return [self firstView]; } return [self sectionView:section]; } -(UIView *)firstView { UIView *contentView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 32)]; UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 15, 40, 15)]; label.text=@"--1--"; [contentView addSubview:label]; return contentView; } -(UIView *)sectionView:(NSInteger)section { UIView *contentView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 32)]; UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom]; btn.frame=CGRectMake(375-50, 15, 30, 16); btn.tag=section; [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 15, 40, 15)]; label.text=@"测试"; if(flag[section]) { [ btn setBackgroundImage:[UIImage imageNamed:@"open"] forState:UIControlStateNormal]; } else { [ btn setBackgroundImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal]; } [contentView addSubview:btn]; [contentView addSubview:label]; contentView.layer.borderColor=[UIColor lightGrayColor].CGColor; contentView.layer.borderWidth=1.0; contentView.alpha=1.0; return contentView; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 40; } -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath { return 2; } -(void)btnClick:(UIButton *)sender { int index=(int)sender.tag; flag[index]=!flag[index]; [_myTableView reloadData]; } @end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。