如何点击uitableview 的HeaderInSection隐藏或展开自section
1个回答
推荐于2016-11-30
展开全部
首先定义一个标识符数组,用来表示每个section的展开收起状态
设置每个section的row个数的方法中,加入一个对标识符的判断
判断为运山 展开,即按照旁胡中设定的个数来显示row(每个section的row个数,可以存在一个数组里)
判断为收起,则返回0
在header上添加button,绑定方法
方法中,根据button的tag值来判断点击的是哪个section,然后对响应section的标识符进行更改
基本流程就这样了
我写的练习代码供参考(没粘贴变量的声明部分,新手,见笑了...):
#pragma mark - Other Method
//点击header的方法
-(void)tapHeader:(UIButton *)sender
{
if ([[openedInSectionArr objectAtIndex:sender.tag - 100] intValue] == 0) {
[openedInSectionArr replaceObjectAtIndex:sender.tag - 100 withObject:@"1"];
NSLog(@"%d打开",sender.tag);
}
else
{
[openedInSectionArr replaceObjectAtIndex:sender.tag - 100 withObject:@"0"];
NSLog(@"%d关闭",sender.tag);
}
[myTableView reloadData];
}
#pragma mark - TableView Method
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [mySectionArr count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView * mySectionView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,320,40)] autorelease];
mySectionView.backgroundColor = [UIColor orangeColor];
UIButton * myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.imageView.image = [UIImage imageNamed:@"淡蓝头.png"做弊];
myButton.frame = CGRectMake(0,0,320,40);
myButton.tag = 100 + section;
[myButton addTarget:self action:@selector(tapHeader:) forControlEvents:UIControlEventTouchUpInside];
[mySectionView addSubview:myButton];
UILabel * myLabel = [[UILabel alloc] initWithFrame:CGRectMake(35,0,150,30)];
myLabel.backgroundColor = [UIColor clearColor];
myLabel.text = [mySectionArr objectAtIndex:section];
[myButton addSubview:myLabel];
[myLabel release];
return mySectionView;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 判断section的展开收起状态
if ([[openedInSectionArr objectAtIndex:section] intValue] == 1) {
return [[rowOfSectionArr objectAtIndex:section] intValue];
}
return 0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.navigationController pushViewController:memberInfoVC animated:YES];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * string = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string] autorelease];
}
cell.textLabel.text = [mySectionArr objectAtIndex:indexPath.section];
return cell;
}
#pragma mark - View lifecycle
// Implement loadView to create a view hierarchy programmatically,without using a nib.
- (void)loadView
{
UIView * myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
myView.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.view = myView;
[myView release];
memberInfoVC = [[MemberInfo alloc] init];
// sectionheader文字
mySectionArr = [[NSMutableArray alloc] initWithObjects:@"猪头",@"笨蛋",@"傻瓜",@"神经病",nil];
// 每个section中的row个数
rowOfSectionArr = [[NSMutableArray alloc] initWithObjects:@"2",@"3",@"4",@"2",nil];
// 每个section展开收起状态标识符
openedInSectionArr = [[NSMutableArray alloc] initWithObjects:@"0",@"0",@"0",@"0",nil];
myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,320,418) style:UITableViewStylePlain];
myTableView.dataSource = self;
myTableView.delegate = self;
[self.view addSubview:myTableView];
[myTableView release];
}
设置每个section的row个数的方法中,加入一个对标识符的判断
判断为运山 展开,即按照旁胡中设定的个数来显示row(每个section的row个数,可以存在一个数组里)
判断为收起,则返回0
在header上添加button,绑定方法
方法中,根据button的tag值来判断点击的是哪个section,然后对响应section的标识符进行更改
基本流程就这样了
我写的练习代码供参考(没粘贴变量的声明部分,新手,见笑了...):
#pragma mark - Other Method
//点击header的方法
-(void)tapHeader:(UIButton *)sender
{
if ([[openedInSectionArr objectAtIndex:sender.tag - 100] intValue] == 0) {
[openedInSectionArr replaceObjectAtIndex:sender.tag - 100 withObject:@"1"];
NSLog(@"%d打开",sender.tag);
}
else
{
[openedInSectionArr replaceObjectAtIndex:sender.tag - 100 withObject:@"0"];
NSLog(@"%d关闭",sender.tag);
}
[myTableView reloadData];
}
#pragma mark - TableView Method
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [mySectionArr count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView * mySectionView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,320,40)] autorelease];
mySectionView.backgroundColor = [UIColor orangeColor];
UIButton * myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.imageView.image = [UIImage imageNamed:@"淡蓝头.png"做弊];
myButton.frame = CGRectMake(0,0,320,40);
myButton.tag = 100 + section;
[myButton addTarget:self action:@selector(tapHeader:) forControlEvents:UIControlEventTouchUpInside];
[mySectionView addSubview:myButton];
UILabel * myLabel = [[UILabel alloc] initWithFrame:CGRectMake(35,0,150,30)];
myLabel.backgroundColor = [UIColor clearColor];
myLabel.text = [mySectionArr objectAtIndex:section];
[myButton addSubview:myLabel];
[myLabel release];
return mySectionView;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 判断section的展开收起状态
if ([[openedInSectionArr objectAtIndex:section] intValue] == 1) {
return [[rowOfSectionArr objectAtIndex:section] intValue];
}
return 0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.navigationController pushViewController:memberInfoVC animated:YES];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * string = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string] autorelease];
}
cell.textLabel.text = [mySectionArr objectAtIndex:indexPath.section];
return cell;
}
#pragma mark - View lifecycle
// Implement loadView to create a view hierarchy programmatically,without using a nib.
- (void)loadView
{
UIView * myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
myView.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.view = myView;
[myView release];
memberInfoVC = [[MemberInfo alloc] init];
// sectionheader文字
mySectionArr = [[NSMutableArray alloc] initWithObjects:@"猪头",@"笨蛋",@"傻瓜",@"神经病",nil];
// 每个section中的row个数
rowOfSectionArr = [[NSMutableArray alloc] initWithObjects:@"2",@"3",@"4",@"2",nil];
// 每个section展开收起状态标识符
openedInSectionArr = [[NSMutableArray alloc] initWithObjects:@"0",@"0",@"0",@"0",nil];
myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,320,418) style:UITableViewStylePlain];
myTableView.dataSource = self;
myTableView.delegate = self;
[self.view addSubview:myTableView];
[myTableView release];
}
大雅新科技有限公司
2024-11-19 广告
2024-11-19 广告
这方面更多更全面的信息其实可以找下大雅新。深圳市大雅新科技有限公司从事KVM延长器,DVI延长器,USB延长器,键盘鼠标延长器,双绞线视频传输器,VGA视频双绞线传输器,VGA延长器,VGA视频延长器,DVI KVM 切换器等,优质供应商,...
点击进入详情页
本回答由大雅新科技有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询