2013-03-13 26 views
3

我有一個帶有兩個部分的UITableView。基於用戶交互(選擇和取消選擇),我的數據源和UITableView被更新以在部分之間移動數據。最初他們只是0節的數據。當我點擊一個單元格時,willSelectCellAtIndexPathdidSelectCellAtIndexPath被調用。正如預期的那樣,當我再次點擊同一個單元時,didDeselectCellAtIndexPath被調用。當在UITableView中點擊已經選擇的單元格時,將調用willSelectCellAtIndexPath

即使在我開始將數據移動到第1部分並選擇和取消選擇後,UITableView's委託方法被適當調用。

將所有數據移至第1部分後,UITableView開始表現出奇怪的行爲。我最初可以選擇一個電話並調用didSelectCellAtIndexPath。但是,當我再次點擊它時,didDeselectCellAtIndexPath永遠不會被調用。取而代之的是,選擇的小區上的任何水龍頭(我已經證實它是通過在第1 [tableView indexPathsForSelectedCells]或任何其他細胞確實只選擇導致willSelectIndexPathdidSelectIndexPath獲取調用。

我有相當多的代碼在這些委託方法,其是不相關的(我相信)......我不明確更改單元格的選定狀態的任何地方我已經發布willSelect方法,必要時可以發佈更多

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
if (remainingItemIsSelected && indexPath.section == 0) { 
     //other cells in the remaining items section are selected and a cell from that section is being selected 

     NSMutableIndexSet *arrayIndexesToBeDeleted = [[NSMutableIndexSet alloc] init]; 
     for (NSIndexPath *previouslySelectedIndexPath in [tableView indexPathsForSelectedRows]) { 
      if (((ReceiptItem *)[self.remainingReceiptItems objectAtIndex:previouslySelectedIndexPath.row]).allocated == YES) { 

       //update data sources 
       NSLog(@"%@%i%@,%i",@"Section #:",previouslySelectedIndexPath.section,@" Row #:",previouslySelectedIndexPath.row); 
       [self.assignedReceiptItems addObject:[self.remainingReceiptItems objectAtIndex:previouslySelectedIndexPath.row]]; 
       [arrayIndexesToBeDeleted addIndex:previouslySelectedIndexPath.row]; 

       //update index path arrays 
       [self.receiptItemsToDeleteIndexPaths addObject:previouslySelectedIndexPath]; 
       [self.receiptItemsToAddIndexPaths addObject:[NSIndexPath indexPathForRow:self.assignedReceiptItems.count-1 inSection:1]]; 

       //update the pressed indexpath to equal to resulting indexpath to pass on to the didSelect method 
       if (previouslySelectedIndexPath.row < indexPath.row) { 
        indexPath = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:0]; 
       } 
      } 
     } 
     //Delete assigned items from the remaining receipt items 
     [self.remainingReceiptItems removeObjectsAtIndexes:arrayIndexesToBeDeleted]; 
     //update table (move allocated item down) 
     [tableView beginUpdates]; 
     [tableView deleteRowsAtIndexPaths:self.receiptItemsToDeleteIndexPaths withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [tableView insertRowsAtIndexPaths:self.receiptItemsToAddIndexPaths withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [tableView endUpdates]; 
     if (self.remainingReceiptItems.count == 0) { 
      [tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic]; 
     } 
     //other cells in the remaining items section are selected and a cell from assigned items is being selected 
     return nil; 
    } 
    return indexPath; 
} 
+0

您是否設置了斷點來檢查方法是否被調用?或者你只考慮你的'NSLog()'語句? – CainaSouza 2013-03-13 20:50:42

+0

我設置了斷點並確認沒有調用'didDeselectRowAtIndexPath'。另外,我確認'willDeselectRowAtIndexPath'也沒有被調用。 – IkegawaTaro 2013-03-13 21:37:45

回答

1

Documentation for UITableViewDelegate:。

tableView:willDeselectRowAtIndexPath:
僅當用戶嘗試選擇不同行時存在現有選擇時,纔會調用此方法。委託人將爲此前選定的行發送此方法。

如果你認爲通過你會發現你遇到的是預期的行爲。攻絲選擇的行是而不是在此行上調用will/didDeselctRowAtIndexPath

取而代之,您可以在didSelectRowAtIndexPath中爲所選行處理此問題,即在此取消選擇該行。

可能的替代

話雖這麼說,我覺得你濫用UITableView類。這實際上並不是爲了做這些移動的東西而設計的。您毫無疑問地注意到自己必須編寫大量代碼才能完成這項工作 - 這是您遇到棘手錯誤的原因。

在我看來,一個更清潔(並最終更靈活)的解決方案將是有兩個單獨的表(或其他)視圖通過代表通知代表datasource變化。也許有更多的工作設置它,但肯定會少得多路上的麻煩。

+0

謝謝。有趣的是,如果你將'allowMultipleSelection'設置爲YES,那麼上面的文檔不支持(在選定單元格上點擊)調用'will/didDeselectRowAtIndexPath'。我想盡可能做到乾淨/靈活,但我不明白單獨的表格視圖如何解決這個問題。在tableview委託方法中,我不是現在需要條件來決定'UITableView'正在調用哪個'section'的值嗎?我是否需要爲每個不同於它們所包含的ViewController的.m文件的數據源和委託設置? – IkegawaTaro 2013-03-13 23:57:03

+0

最簡單的方法是不用爲多個表視圖處理單個數據源(這是可能的 - 請參閱例如Apple搜索結果控制器的代碼)就是讓一個控制器控制兩個表視圖控制器。 – Mundi 2013-03-14 08:07:43

相關問題