如何隱藏UITableView中的一項

我最近工做中的一個iOS App中常常有在不一樣的場合,隱現菜單列表裏某一項的需求.
若是初始化的時候就去掉某一項的話,有可能讓序號變化, 處理上會比較麻煩容易出錯.
我採用了初始化列表相同可是隱藏section的方式,保持序號不變,方便處理.
那麼如何隱藏一個section呢?
其實很簡單,就是將section的高度設置爲0
重載 heightForRowAtIndexPath方法, 假設要隱藏section 1的話,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 1)
return 0;
else
return 60;
}
簡單的列表這樣就能夠了.
若是你的菜單項帶有header和footer, 也須要將他們隱藏, 同理重載heightForHeaderInSection 和 heightForFooterInSection 方法io

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if(section == 1)
return 0.01;
else
return 18;
}table

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if(section == 1)
return 0.01;
else
return 16;
}float

注意: 這裏沒有return 0, 而是return 0.01, 是由於這兩個方法不接受0值, 返回0會不起做用. 由於是返回float類型, 因此返回一個較小的數,好比0.01之類的.
另外.若是你派生了viewForFooterInSection 或 viewForHeaderInSection, 隱藏的section要返回nil,不然隱藏不了, 這一點也很重要.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if (section == 0) {
return footView0;
} else if (section == 1) {
return nil;
} else if (section == 2) {
return footView2;
} else if (section == 3) {
return footView3
} else if (section == 4)
return footView4;
return nil;
}方法

viewForHeaderInSection也是同理, 這樣就能夠隱藏一個section及其header和footer了tab

相關文章
相關標籤/搜索