2012-04-25 11 views
9

これは簡単ですが問題があります。ストーリーボードで作成したスタティックUITableViewからcellを削除するには

私は、必要でなければプログラムで削除したいセルを持つ静的なUITableViewを持っています。

私はそれ

IBOutlet UITableViewCell * cell15; 

ためにIBOutletを持っていると私は「これはそれを隠しますが、私はすることができ、細胞がために使用空白を残し、

cell15.hidden = true; 

を呼び出すことによって、それを削除することができますそれを取り除く。

おそらくハックは、その高さを0に変更することでしょうか?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:indexPath 
{ 
//what would I put here? 
} 

ありがとうございます!

+1

'tableView:deleteRowAtIndexPath:'はどうですか? 自分自身で試してみませんでしたか、すぐに試してみてください – anticyclope

+0

ありがとう!削除したい行を選択するにはどうすればいいですか? – dot

+0

[StoryBoardで設計されたUITableViewから静的セルを削除する方法](https://stackoverflow.com/questions/8262270/how-to-remove-a-static-cell-from-a-uitableview-designed) -in-storyboard) –

答えて

12

静的テーブルでは、データソースメソッドを実装していないので、実際にはデータソースでこれを処理することはできません。高さは行く方法です。

これを試してみてください:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    if (cell == cell15 && cell15ShouldBeHidden) //BOOL saying cell should be hidden 
     return 0.0; 
    else 
     return [super tableView:tableView heightForRowAtIndexPath:indexPath]; 
} 

更新

自動レイアウトの下で、これが最善の解決策ではないかもしれない、と思われます。代わりの回答hereが役に立ちます。

+0

この場合、私は 'BAD_ACCESS'を取得しました。 TableViewはセルがインスタンス化される前に高さを要求していませんか? – Besi

+0

これは前に尋ねます(この場合、セルはゼロになり、スーパーに落ちます)。スクロール中にも、このコードでどのように悪用されるのか分かりません。新しい質問を投稿し、この回答へのリンクを付ける必要があります。 – jrturton

+5

私はまた、ある種の無限ループによって引き起こされた 'BAD_ACCESS'を取得しました。 'if(indexPath.row == 3 && cellShouldBeHidden)' – codingFriend1

3

テーブルがどのように動作するかによって、データソースでは、tableView:numberOfRowsInSection:を実装して、必要なロジックに基づいてセクションに0行を戻すことができます。

は、あなたのコメントのための更新:

あなたの実装がそのように、あなたが必要とするすべてはあなたが隠し/削除アリ行を持つセクションを処理するためのスイッチで呼び出されたときセクション・パラメータは、iOS版によって移入されます。以下の例:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    switch(section) { 
     case 0: // first section of your table, change for your situation 
      return 0; 
     default: 
      return 0; 
    } 
} 
+0

コードのセクションを選択する方法は?これは私が本当に問題を抱えているものです... – dot

+0

これは隣接する2つの(隠れていない)セクションの間に大きすぎる隙間を残します。 – Drux

6

あなたは、静的tableview細胞を隠す/表示するには、セル識別子とtableView:willDisplayCelltableView:heightForRowAtIndexPathを使用することができますが、ヨーヨーはないselfに、superを参照heightForRowAtIndexPathを実装する必要があります。これらの2つの方法は、私のために正常に動作:

(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if ([cell.reuseIdentifier.description isEqualToString:@"cellCelda1"]) { 
    [cell setHidden:YES]; 
    } 
} 

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath]; 
    if ([cell.reuseIdentifier.description isEqualToString:@"cellCelda1"]) { 
     return 0; 
} 
    return cell.frame.size.height; 
} 
+0

完璧に動作します! – mikemike396

0

それだけで一定のセルに

-(void)tableViewSearchPeopleCellHide:(BOOL)hide{ 

    searchCellShouldBeHidden=hide; 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]]; 
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
    cell.hidden=hide; 
    self.searchPeople.hidden=hide;//UILabel 
    [self.tableView reloadData]; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    if (searchCellShouldBeHidden) //BOOL saying cell should be hidden 
     return 0.0; 
    else 
     return [super tableView:tableView heightForRowAtIndexPath:indexPath]; 
} 
0

をあなたが行うことができますまず最初にあなたが望むストーリーボードからセルのタグです隠れる。 識別できる標準番号を入力してください。

このコードを追加してください。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath]; 
    if (cell.tag==10) { //I have put 10 for some static cell.  
       cell.hidden=YES; 
       return 0;   

    } 
    cell.hidden = NO; 
    return [super tableView:tableView heightForRowAtIndexPath:indexPath]; 
} 
0

非表示にするセルをコードのどこかに隠すように設定します。このコードを追加:(あなたのセルの行の高さが異なる場合は、より多くの関数をオーバーライドする必要があります)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    int rowCount=0; 
    for (int row=0; row<[super tableView:tableView numberOfRowsInSection:section]; ++row){ 
     NSIndexPath* path=[NSIndexPath indexPathForRow:row inSection:section]; 
     UITableViewCell* cell=[super tableView:tableView cellForRowAtIndexPath:path]; 
     if (!cell.hidden){ 
      ++rowCount; 
     } 
    } 
    return rowCount; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    int realRow=-1; 
    for (int row=0; row<[super tableView:tableView numberOfRowsInSection:indexPath.section]; ++row){ 
     NSIndexPath* path=[NSIndexPath indexPathForRow:row inSection:indexPath.section]; 
     UITableViewCell* cell=[super tableView:tableView cellForRowAtIndexPath:path]; 
     if (!cell.hidden){ 
      ++realRow; 
     } 
     if (realRow==indexPath.row) 
      return cell; 
    } 
    return nil; 
} 
関連する問題