我正在尝试使用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
对象的方法。
希望对您有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句