您的位置:首页 > 产品设计 > UI/UE

UITableView section header 显示在最上面

2014-02-28 12:00 441 查看
或许大家都是有过这样的情况:

在一个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的展开之间互不干扰,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: