2011-04-24 64 views
1

我創建了一個可編輯的UITableView,它支持我自己設計的幾個自定義UITableViewCells。每當用戶創建新記錄或修改現有記錄時,他們都使用此UITableView。我使用分組樣式表視圖將表視圖分成了多組tableViewCells。當numberOfRowsInSection返回0時,UITableView差距

當用戶正在編輯記錄時,我不希望第1部分顯示。爲了達到這個目的,我在調用numberOfRowsInSection方法時返回0。一切正常,但是第0節和第2節之間存在「輕微的視覺差距」,如果可能的話,我想盡量消除。我想避免重新編碼表視圖控制器動態處理indexPaths。我的很多indexPath(&行)都是硬編碼的。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

printf("CustomEntryViewController, -tableView:numberOfRowsInSection:\n"); 

if (tableView == self.customEntryTableView) { 

    if (section == 0) 

     return 1; 

    else if (section == 1) { 

     if ([self.entryMode isEqualToString:@"ADD"]) 
      return 2; 
     else if ([self.entryMode isEqualToString:@"EDIT"]) 
      return 0; 

    } 
    else if (section == 2) 

     return 1; 

    else if (section == 3) 

     return 1; 

    else if (section == 4 && self.uisegQuantityAnswer.selectedSegmentIndex == 0) 

     return 1; 

} 

return 0; 
} 

在此先感謝。

回答

1

我找到了解決問題的方法。我在視圖控制器中創建了兩個輔助方法來虛擬化tableView委託傳給我的部分。每當我打算在我的表視圖中禁用某個節時,虛擬節會導致我寫入的邏輯被跳過。

- (NSIndexPath *)virtualIndexPath:(NSIndexPath *)indexPath { 

    return [NSIndexPath indexPathForRow:indexPath.row inSection:[self virtualSection:indexPath.section]]; 

} 


- (NSUInteger)virtualSection:(NSUInteger)section { 

    NSUInteger virtualSection; 

    if ([self.entryMode isEqualToString:@"ADD"]) 

     virtualSection = section; 

    else if ([self.entryMode isEqualToString:@"EDIT"]) { 

     if (section == 0) 
      virtualSection = section; 

     else if (section > 0) 
      virtualSection = section + 1; 

    } 

    return virtualSection; 

} 

然後,我在整個應用程序的其餘部分調用上述方法之一。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    NSUInteger virtualSection = [self virtualSection:section]; 

    if (tableView == self.customEntryTableView) { 

     if (virtualSection == 0) { 

// Additional code.... 

除此之外我還修改-tableView:numberOfSectionsInTableView:方法返回部分的正確數目每ADD與編輯模式。

由於這只是一個解決方法,我會保持這個問題未被回答。

+0

雖然不像只說「0行,沒有頁腳/頁眉空白」那麼簡潔,但只要您記得重新映射這些部分,這是很好的和可預測的。 – 2011-05-27 12:23:30

相關問題