美文网首页
ios 实现一个tableView的伸展和收缩

ios 实现一个tableView的伸展和收缩

作者: 天下林子 | 来源:发表于2018-04-12 15:30 被阅读34次

实现一个表格有多个section,在该头上有一个点击的按钮,箭头上下切换

当直接在section的headerView上写的时候,当刷新表格,会重新创建箭头按钮,故会有问题.....你懂得
实现方式,给出关键几部,

写给自己看,,,,

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    BBSUIForumHeaderFooterView *headerView = [BBSUIForumHeaderFooterView sectionHeadViewWithTableView:tableView section:section allData:self.allDataArr];
    headerView.deleagte = self;
    headerView.sectionTag = section;
    [headerView updateHeaderView:self.allDataArr];
    
    if (section > 0) {
        NSArray *arr = self.allDataArr[section - 1];
        BBSForum *forum = arr[0];
        headerView.isclicked = forum.isExpect;
    }
    
    return headerView;
}
#pragma mark - BBSUIForumHeaderFooterView delegate
- (void)expectForumHeaderView:(NSInteger)section
{
    NSArray *expectArr = self.allDataArr[section - 1];
    [expectArr enumerateObjectsUsingBlock:^(BBSForum *model, NSUInteger idx, BOOL * _Nonnull stop) {
        model.isExpect = !model.isExpect;
    }];
    [self.forumTableView reloadData];
}


@protocol  BBSUIForumHeaderFooterViewDelegate<NSObject>

- (void)editForumHeaderView;
- (void)expectForumHeaderView:(NSInteger)section;

@end


@interface BBSUIForumHeaderFooterView : UITableViewHeaderFooterView

@property (nonatomic, assign) NSInteger sectionTag;
@property (nonatomic, weak) id <BBSUIForumHeaderFooterViewDelegate> deleagte;

+ (instancetype)sectionHeadViewWithTableView:(UITableView *)tableView section:(NSInteger)section allData:(NSArray *)allData;

- (void)updateHeaderView:(NSArray *)allData;

@property (nonatomic, assign) BOOL isclicked;

@end
#pragma mark - init
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        [self _initViews];
    }
    return self;
}

+ (instancetype)sectionHeadViewWithTableView:(UITableView *)tableView section:(NSInteger)section allData:(NSArray *)allData
{
    BBSUIForumHeaderFooterView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headIdentifier];
    if (!headView) {
        headView = [[BBSUIForumHeaderFooterView alloc] initWithReuseIdentifier:headIdentifier];
    }
    
    return headView;
}

#pragma mark - UI
- (void)_initViews
{
   .........
}
==================@@@@@@@@@@@@@=============
这个很重要
-(void)setIsclicked:(BOOL)isclicked
{
    _isclicked = isclicked;
    if (isclicked) {
        _setButton.selected = YES;
    }else {
        _setButton.selected = NO;
    }
    
}
==================@@@@@@@@@@@@@=============
- (void)updateHeaderView:(NSArray *)allData
{
    if (self.sectionTag == 0) {
        [self.titleLabel setFrame:CGRectMake(15, 0, DZSUIScreen_width, 32)];
        [self.titleLabel setText:@"置顶版块"];
        self.setButton.hidden = YES;
        self.editButton.hidden = NO;
    }
    else
    {
        [self.titleLabel setFrame:CGRectMake(15, -17, DZSUIScreen_width, 32)];
        NSArray *dicArr = allData[self.sectionTag - 1];
        [dicArr enumerateObjectsUsingBlock:^(BBSForum *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self.titleLabel setText:obj.fupp];
        }];
        
        self.editButton.hidden = YES;
        self.setButton.hidden = NO;
    }
}

#pragma mark - 编辑
- (void)_editButtonHandler:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if ([self.deleagte respondsToSelector:@selector(editForumHeaderView)]) {
        [self.deleagte editForumHeaderView];
    }
}

#pragma mark - 展开收回
- (void)_setButtonHandler:(UIButton *)sender
{
     sender.selected = !sender.selected;
    if ([self.deleagte respondsToSelector:@selector(expectForumHeaderView:)]) {
        [self.deleagte expectForumHeaderView:self.sectionTag];
    }
    
}

相关文章

网友评论

      本文标题:ios 实现一个tableView的伸展和收缩

      本文链接:https://www.haomeiwen.com/subject/tctfkftx.html