我正在尝试使用tableview标头中的手风琴表
我的示例代码如下。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
headerView.tag = section;
headerView.backgroundColor = [UIColor whiteColor];
UILabel *headerString = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, self.view.frame.size.width-20-50-20, 50)];
headerString.textColor = [UIColor blackColor];
BOOL manyCells = [[arrayForBool objectAtIndex:section] boolValue];
if (!manyCells) {
headerString.textColor=[UIColor blackColor];
}
else
{
headerString.textColor=[UIColor blueColor];
}
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, 40, 40)];
imgView.contentMode = UIViewContentModeScaleAspectFit;
imgView.image = [UIImage imageNamed:imgarray[section]];
headerString.text=sectionTitleArray[section];
headerString.textAlignment = NSTextAlignmentLeft;
headerString.font=[UIFont systemFontOfSize:20];
[headerView addSubview:headerString];
[headerView addSubview:imgView];
UITapGestureRecognizer *headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
[headerView addGestureRecognizer:headerTapped];
UIImageView *upDownArrow = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"downArrowBlack"]];
upDownArrow.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
upDownArrow.frame = CGRectMake(self.view.frame.size.width-40, 10, 20, 20);
[headerView addSubview:upDownArrow];
if([headerString.text isEqualToString:@"Contact Us"])
{
upDownArrow.hidden=YES;
}
//return headerView;
}
return headerView;
}
和我的sectionHeaderTapped:
- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
if (indexPath.row == 0) {
BOOL collapsed = [arrayForBool[indexPath.section] boolValue];
collapsed = !collapsed;
arrayForBool[indexPath.section] = @(collapsed);
//reload specific section animated
NSRange range = NSMakeRange(indexPath.section, 1);
NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
[self.tableView1 reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade];
}
}
如果打开一个标题意味着我想在该tableview中折叠其他标题。如何做到这一点?
提前帮助我。
最佳答案
我以略有不同的方式实现了相同的目标。让我逐步解释。
1)我已经创建了一个UIView
子类MYCollapsableTableHeaderView
,它的MYCollapsableTableHeaderViewDelegate
声明与此完全相同。
#import <UIKit/UIKit.h>
@class MYCollapsableTableHeaderView;
@protocol MYCollapsableTableHeaderViewDelegate <NSObject>
@required
- (void)collapsableTableHeaderViewTapped:(MYCollapsableTableHeaderView *)collapsableTableHeaderView;
@end
@interface MYCollapsableTableHeaderView : UIView
@property (assign, nonatomic) id <MYCollapsableTableHeaderViewDelegate> collapsableTableHeaderViewDelegate;
@property (nonatomic) NSUInteger section;
@property (nonatomic, getter = isExpanded) BOOL expanded;
+ (instancetype)collapsableTableHeaderViewWithCollapsableTableHeaderViewDelegate:(id <MYCollapsableTableHeaderViewDelegate>)delegate;
- (CGFloat)collapsableTableHeaderViewHeight;
- (void)giveText:(NSString *)text;
@end
2)填充必要数量的
MYCollapsableTableHeaderView
对象,并在NSMutableArray
子类中借助UITabelVewController
保留引用。MYCollapsableTableHeaderView *collapsableTableHeaderView = [MYCollapsableTableHeaderView collapsableTableHeaderViewWithCollapsableTableHeaderViewDelegate:self];
[headerViews addObject:collapsableTableHeaderView];
3)完成后,我使用了
UITableView
委托方法- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
MYCollapsableTableHeaderView *collapsableTableHeaderView = headerViews[section];
[collapsableTableHeaderView setSection:section];
[collapsableTableHeaderView giveText:[self tableView:tableView titleForHeaderInSection:section]];
return collapsableTableHeaderView;
}
集成
MYCollapsableTableHeaderView
对象。4)根据方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
我检查像这样的isExpanded属性
if ([headerViews[section] isExpanded]) return [currentSection.items count];
else return 0;
其中headerViews是保留所有
NSMutableArray
对象的MYCollapsableTableHeaderView
。5)用
MYCollapsableTableHeaderViewDelegate
实现- (void)collapsableTableHeaderViewTapped:(MYCollapsableTableHeaderView *)collapsableTableHeaderView {
NSUInteger section = [collapsableTableHeaderView section];
BOOL isExpanded = [collapsableTableHeaderView isExpanded];
for (MYCollapsableTableHeaderView headerView* in headerViews) {
[headerView setExpanded:NO] // here you are collapsing all headerviews.
}
[headerViews[section] setExpanded:YES]; // adding and exception for the selected one
[self.tableView reloadData];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:isExpanded? UITableViewRowAnimationBottom:UITableViewRowAnimationMiddle];
}
而已。
注意:我更喜欢使用
UIView's
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self setExpanded:!_expanded];
if ([self.collapsableTableHeaderViewDelegate respondsToSelector:@selector(collapsableTableHeaderViewTapped:)]) [self.collapsableTableHeaderViewDelegate collapsableTableHeaderViewTapped:self];
}
检测触摸的方法,而不是添加
UITapGestureRecognizer
对象。希望对您有帮助。