2013-04-10 53 views
0

我有一個UITableView,我想在用戶點擊添加按鈕時動態添加部分標題。如果用戶不需要它,用戶應該也可以刪除標題。在每個標題下,用戶應該能夠添加相關的項目列表。除此之外,用戶應該只能在選定部分動態插入行。請提出一些想法來實現此功能。提前致謝。在UITableView中插入動態部分標題

+0

- 1不提供任何輸入到您嘗試過的解決方案 – Aaron 2013-08-05 03:53:11

回答

0

我會告訴動態部分標題。保持一個標誌,並初始設置爲0。當用戶點擊該按鈕中的buttonClick方法設置標誌爲1並重新加載tableView。

  -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
      { 
        if(flag) 
         return aView;//alloc init aView and return. 
        else 
         return nil. 
      } 

同樣,對於動態行有一個數組和numberOfRows是array.count。 點擊按鈕在數組中插入另一個項目並重新載入表格。

我希望這個幫助。

0

當用戶點擊按鈕,此方法設置一個標誌,並刷新你的tableview,然後做吧..

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    if(flag) 
    { 
     UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 30)]; 
     return headerView; 
    } 
    else 
     return nil; 
} 

,你可以在這樣的表視圖刪除頭......

如果要刪除表格的標題視圖,只需將myTable.tableHeaderView屬性設置爲nil即可。如果你有什麼實際上是一個節頭,那麼你需要返回nil從viewForHeaderInSection方法,並調用[myTableView reloadData]

,我希望這會正常工作。

2

您可以通過刪除,然後重新添加該部分實現這一目標,這將導致tableView:titleForHeaderInSection:

通話假設你的部分指數爲0:

BOOL shouldShowHeader; 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    shouldShowHeader ? @"Your Header" : nil; 
} 

- (IBAction)buttonAction:(id)sender 
{ 
    shouldShowHeader = !shouldShowHeader; 
    NSIndexSet *set = [NSIndexSet indexSetWithIndex:0]; 
    [self.tableView beginUpdates]; 
    [self.tableView deleteSections:set withRowAnimation:UITableViewRowAnimationNone]; 
    [self.tableView insertSections:set withRowAnimation:UITableViewRowAnimationNone]; 
    [self.tableView endUpdates]; 
} 

頁腳的工作方式相同。