2011-12-15 11 views
5

スタティックセルを含むテーブルビュー内のセクションを削除または非表示にしようとしています。私は機能でそれを隠そうとしていますviewDidLoad。コードは次のとおりです。スタティックセルテーブルビューからセクションを削除する方法

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.tableView beginUpdates]; 
    [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:YES]; 
    [self.tableView endUpdates]; 

    [self.tableView reloadData]; 
} 

セクションが表示されます。私はそれでストーリーボードを使用しています。あなたは私を助けてくれますか?ありがとう!

答えて

-2

reloadDataは、テーブルビューでdataSourceを再読み込みするようです。また、reloadDataを呼び出す前にdataSourceからデータを削除する必要があります。配列を使用している場合は、removeObject:でオブジェクトを削除してからreloadDataを呼び出してください。

6

numberOfRowsInSectionをオーバーライドすると、セクションを非表示にすると最も便利です。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section == 1) 
     // Hide this section 
     return 0; 
    else 
     return [super tableView:self.tableView numberOfRowsInSection:section]; 
} 
+0

これは私の目的には適していますが、これを行った後に垂直方向のスペースが残ります。 – guptron

+0

OPが望んでいたこと、まさに魂! – Tumtum

0

ここに移動します。これは、垂直方向のスペースも削除します。

NSInteger sectionToRemove = 1; 
CGFloat distance = 10.0f; // minimum is 2 since 1 is minimum for header/footer 
BOOL removeCondition; // set in viewDidLoad 

/** 
* You cannot remove sections. 
* However, you can set the number of rows in a section to 0, 
* this is the closest you can get. 
*/ 

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section { 
    if (removeCondition && section == sectionToRemove) 
    return 0; 
    else 
    return [super tableView:self.tableView numberOfRowsInSection:section]; 
} 

/** 
* In this case the headers and footers sum up to a longer 
* vertical distance. We do not want that. 
* Use this workaround to prevent this: 
*/ 
- (CGFloat)tableView:(UITableView *)tableView 
    heightForFooterInSection:(NSInteger)section { 

    return removeCondition && 
       (section == sectionToRemove - 1 || section == sectionToRemove) 
      ? distance/2 
      : distance; 
} 

- (CGFloat)tableView:(UITableView *)tableView 
    heightForHeaderInSection:(NSInteger)section { 

    return removeCondition && 
       (section == sectionToRemove || section == sectionToRemove + 1) 
      ? distance/2 
      : distance; 
} 
関連する問題