2013-02-22 50 views
0

我有一個表格視圖,其單元格包含UISwitch。對於其中的一個,當開關打開時,我想在表格視圖中顯示一行。我現在通過在我的tableView:numberOfRowsInSection:tableView:cellForRowAtIndexPath:方法中設置條件並在交換機的事件處理程序中調用reloadData來完成此操作。這工作正常。不過,我想爲表格單元格進行動畫或動畫製作,並且我無法弄清楚如何在現有的表格視圖插入/刪除動畫API中工作。通過UI動作動畫進/出表格視圖單元格

的代碼,我有:

- (IBAction)didToggleContractProperties:(id)sender { 
    UISwitch *toggle = (UISwitch *)sender; 
    switch (toggle.tag) { 
     case 100:  // Packaging requested 
      self.contract.isPackingRequired = toggle.on; 
      [self.tableView reloadData]; 
      break; 
     case 101:  // On-campus pickup 
      self.contract.isOnCampus = toggle.on; 
      break; 
     case 102:  // Insurance requested 
      self.contract.isInsuranceRequested = toggle.on; 
      break; 
     default: 
      break; 
    } 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    if (section == 0) { 
     if (self.contract.isPackingRequired) 
      return 4; 
     else 
      return 3; 
    } 
    else if (section == 1) 
     return 1; 
    else 
     return 1; 
} 

謝謝!

回答

0

這是你將如何做到這一點的isPackRequired開關,例如:

- (IBAction)didToggleContractProperties:(id)sender { 
    UISwitch *toggle = (UISwitch *)sender; 
    switch (toggle.tag) { 
     case 100:  // Packaging requested 
      if (self.contract.isPackingRequired != toggle.on) 
      { 
       self.contract.isPackingRequired = toggle.on; 
       if (toggle.on) 
       { 
        [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] 
              withRowAnimation:UITableViewRowAnimationMiddle]; 
       } 
       else 
       { 
        [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] 
              withRowAnimation:UITableViewRowAnimationMiddle]; 
       } 
      } 
      break; 
     case 101:  // On-campus pickup 
      ... 
     default: 
      break; 
    } 
} 

顯然,改變rowsection是適當的值的insertRowsAtIndexPathsdleeteRowsAtIndexPaths來電,反映您想要插入/刪除此行的位置。

+1

謝謝,這似乎是最完整的答案。我嘗試過'insertRowsAtIndexPaths:',但我忘記了相應的'delete'方法,並且應用程序崩潰,因爲行數不一致。 – FeifanZ 2013-02-22 05:59:24

0

添加動畫表的細胞的方法是本

[tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight]; 

其中的tableView是表格圖,indexPaths是包含新的行的索引路徑的可變數組。

0

當你的開關接通時,只需在你的情況下添加你的數組,添加項目或條件,然後調用這個方法來打算你的部分。這將會產生輸入/輸出效果,以向tableview顯示越來越少的項目。

[mytv reloadSections: [NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade]; 
相關問題