24

Static TableView のセクションを非表示にするこのチュートリアルを見つけました: http://code-ninja.org/blog/2012/02/29/ios-quick-tip-programmatically-hiding-sections-of-a-uitableview -with-static-cells/

セクションまたは行を追加すると、うまく機能しますが、変更しないとうまくいきません。私は初心者で、それを変更することができません。誰かが複数のセクションを非表示にするのを手伝ってくれますか?

どうもありがとう!

4

6 に答える 6

46
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {            
    if (section == 2 && _hideTableSection) {
        //header height for selected section
        return 0.1; 
    } else {
        //keeps all other Headers unaltered 
        return [super tableView:tableView heightForHeaderInSection:section]; 
    }  
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {        
    if (section == 2 && _hideTableSection) {
        //header height for selected section
        return 0.1; 
    } else {
        // keeps all other footers unaltered
        return [super tableView:tableView heightForFooterInSection:section]; 
    } 
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) { //Index number of interested section
        if (hideTableSection) {
            return 0; //number of row in section when you click on hide
        } else {
            return 2; //number of row in section when you click on show (if it's higher than rows in Storyboard, app will crash)
        }
    } else {
        return [super tableView:tableView numberOfRowsInSection:section]; //keeps inalterate all other rows 
    }    
}
于 2013-07-20T16:29:50.377 に答える
18

多くの回答を掘り下げ、多くの不具合にぶつかった後、この問題を解決するために書いたコードを共有したいと思いました。これは xCode 7.2.1 用です。(Swift でのコード例)

私の使用例は、ストーリーボードの静的グループ化された TableView の使いやすさを使用したいというものでしたが、ユーザー プロファイルに基づいて特定のセクションを非表示にする必要がありました。これを機能させるには (他の投稿で説明されているように)、ヘッダーとフッター、セクションの行を非表示にし、ヘッダー/フッターのテキストを非表示にする必要があります (少なくとも上部のセクションで)。テキストを非表示 (透明にする) にしないと、ユーザーはテーブルの上部 (ナビゲーション コントローラーの下) を超えて上にスクロールし、テキストがすべてぎっしり詰まっているのを見ることができることがわかりました。

これを簡単に変更できるようにし、コード全体に条件が広がらないようにしたかったので、shouldHideSection(section: Int) という単一の関数を作成しました。これは、非表示にする行を変更するために変更する必要がある唯一の関数です。

func shouldHideSection(section: Int) -> Bool {
    switch section {
    case 0:  // Hide this section based on condition below
        return user!.isProvider() ? false : true

    case 2:
        return someLogicForHiddingSectionThree() ? false : true

    default:
        return false
    }
}

コードの残りの部分は、shouldHideSection() を呼び出すだけです。

// Hide Header(s)
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return shouldHideSection(section) ? 0.1 : super.tableView(tableView, heightForHeaderInSection: section)
}

// Hide footer(s)
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return shouldHideSection(section) ? 0.1 : super.tableView(tableView, heightForFooterInSection: section)
}

// Hide rows in hidden sections
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return shouldHideSection(indexPath.section) ? 0 : super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}

// Hide header text by making clear
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if shouldHideSection(section) {
        let headerView = view as! UITableViewHeaderFooterView
        headerView.textLabel!.textColor = UIColor.clearColor()
    }
}

// Hide footer text by making clear
override func tableView(tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
    if shouldHideSection(section) {
        let footerView = view as! UITableViewHeaderFooterView
        footerView.textLabel!.textColor = UIColor.clearColor()
    }
}

多くの異なる値 (0、0.1、-1、... を返す) を試して、最終的に満足のいく解決策を得る必要がありました (少なくとも iOS 9.x では)。

これが役立つことを願っています。改善を提案した場合はお知らせください。

于 2016-03-15T05:09:06.987 に答える
2

セクションの高さとして 0 を返すと、Apple API はそれを無視します。したがって、0 より大きい小さな値を返すだけです。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  if (section == 0) {
    return 1;
  }

  return 44;
}

また、ヘッダーにビューを実装し、表示したくないセクションに nil を返します。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  if (section == 0 && !self.personaCells.count) {
    return nil;
  }

  UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)];
  UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, headerView.frame.size.width, 20)];
  NSString *headerTitle = @"SAMPLE TITLE";
  headerLabel.text = headerTitle;    
  [headerView addSubview:headerLabel];
  return headerView;
}
于 2015-07-16T17:20:27.887 に答える
1

セクションの値を 0.01 に設定します。非表示にしたいセクションは、次の方法で試すことができます。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat headerHeight=10.f;
    if (section==0)
    {
        headerHeight=0.01f;
    }
    else
    {
        headerHeight=50.0f;
    }
    return headerHeight;
}
于 2015-04-05T01:21:09.860 に答える
0

ストーリーボードからセクション ヘッダーのタイトルを消去すると、自動的に消えます。それは、タイトルの内容だけでなく、それが占めるスペースも意味します。

于 2016-05-03T15:29:40.220 に答える