UITableView section header 显示在最上面

或许大家都是有过这样的情况:

在一个UITableView中有N个section,然后每个section下面有1个或者多个cell,在section里面添加一个点击事件按钮,在点击之后将对应的section中的cell显示出来,平时是隐藏的。

可是显示出来之后,section并不是显示在tableview 的最上面,而是哪里打开,哪里显示,找了很久找到了这样的方法,将指定的section放在指定的位置,下面贴代码:

//点击section之后,将这个section放到tableview的最上面一行
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:button.tag];
CGRect frame = [tbList rectForSection:indexPath.section];
[tbList setContentOffset:CGPointMake(0, frame.origin.y) animated:YES];

上面的代码放在按钮的点击事件中,点击事件全部如下:

- (void)imgBtnClick:(UIButton *)button {
    
    
    
    iSection = button.tag;
    int sectionIndex = ((UIButton*)button).tag;
    if (sectionIndex<0) {
        sectionIndex = -sectionIndex;
    }
    
    if (flag[sectionIndex]) {
        for (int i=0; i<[dicIntro count]; i++) {
            flag[i]=FALSE;
        }
        if(isClick == TRUE) {
            
            isClick = FALSE;
        }
    }
    else {
        for (int i=0; i<[dicIntro count]; i++) {
            flag[i]=FALSE;
        }
        
        isClick = TRUE;
        flag[sectionIndex] = !flag[sectionIndex];
    }
    [tbList reloadData];
    
    //点击section之后,将这个section放到tableview的最上面一行
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:button.tag];
    CGRect frame = [tbList rectForSection:indexPath.section];
    [tbList setContentOffset:CGPointMake(0, frame.origin.y) animated:YES];
}
大家不要忘记把代码放在【tblist reloadData】之后,这样可以让cell的展开之间互不干扰,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值