2012-04-18 39 views
2

我想展開UITableView部分,我必須顯示UITableViewCells。我該怎麼做才能做到這一點?想擴大和縮小UITableview部分

+2

Google'expandable table view iphone'我有一個視頻鏈接+ 5相關的SO問題,請做一個搜索。謝謝 – iNoob 2012-04-18 06:51:54

回答

6
  • 一個簡單的實現方法是將 部分的單元高度保持爲零。
  • 充分利用viewForSectionHeader可觸摸
  • 當你觸摸它,下段
  • 細胞設定適當的高度寫邏輯部分

OR之間切換,

  • 在觸摸節標題重新加載表中的更新的行計數部分觸摸。

許多其他方式來做到這一點。 Apple example

+0

謝謝Vignesh。我正在嘗試。如果你有其他簡單的例子,那麼對我來說 – Nishi 2012-04-18 06:53:36

+0

@ user1335760會是個好主意。看看蘋果的例子,它做你想做的。 – Vignesh 2012-04-18 06:55:55

+0

+1鏈接到Apple示例 – onmyway133 2013-11-26 07:42:12

0

根據Vignesh的回答,我已經嘗試了第二種解決方案。「在觸摸節標題時,用更新後的行重新載入表計數爲觸摸的節。

首先,聲明一個數組來存儲每個section的isExpanded標籤。初始的,所有的值都是BOOL NO.Means所有的section行被摺疊。

然後,在

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 

方法,通過實施下面的代碼sectionHeader觸摸事件: 因爲我使用作爲區段標題視圖自定義單元格。所以這裏是「細胞」,你可以使用你自己的觀點。

​​

而且,在截取sectionHeader時做些事情。

- (void)sectionTapped:(UITapGestureRecognizer *)recognizer 
{ 

    NSMutableArray *isSectionTouched=[[NSMutableArray alloc]initWithCapacity:_sectionExpandBool.count]; 
    isSectionTouched=[_sectionExpandBool mutableCopy]; 
    if ([[isSectionTouched objectAtIndex:recognizer.view.tag]boolValue]==YES) { 
     [isSectionTouched replaceObjectAtIndex:recognizer.view.tag withObject:[NSNumber numberWithBool:NO]]; 
    }else if ([[isSectionTouched objectAtIndex:recognizer.view.tag]boolValue]==NO){ 
     [isSectionTouched replaceObjectAtIndex:recognizer.view.tag withObject:[NSNumber numberWithBool:YES]]; 
    } 
    _sectionExpandBool=isSectionTouched; 
    [self.tableView reloadData]; 

} 

不要忘記修改numberOfRowsInSection方法。 row.count應根據_sectionExpandBool的值進行更改,如果該部分的ExpandBool爲YES,則應返回正確的數據源編號,否則返回0.

它符合我的期望。不過,我有點擔心內存泄漏或什麼,因爲每次點擊標題時,整個表格視圖都會重新加載。

我不知道是否有一些解決方案只是重新加載特定的部分。謝謝。