2013-11-04 25 views
0

我使用枚舉來填充UITableView。現在在運行時,在獲取服務器數據之後,我需要決定是否填充第一部分。所以,我需要刪除第一個枚舉(MyListSectionType1)條目以使其工作。在運行時定義枚舉以填充UITableView

typedef enum { 
    MyListSectionType1, 
    MyListSectionType2, 
    MyListSectionType3, 
    MyListSectionType4, 
    MyListSectionType5, 
    MyListSectionType6, 
    MyListSectionType7, 

    MyListSectionTypeMax, 

} MyListSectionType; 

我想用下面的代碼嘗試,但如何在運行時定義showShow是另一個問題。我試圖在我得到服務器數據的類中定義它,但這不起作用。任何線索?

typedef enum { 
#ifdef showShow 
    MyListSectionType1, 
#endif 
    MyListSectionType2, 
    MyListSectionType3, 
    MyListSectionType4, 
    MyListSectionType5, 
    MyListSectionType6, 
    MyListSectionType7, 

    MyListSectionTypeMax, 

} MyListSectionType; 
+1

您不能在運行時更改'enum'。實際上,在運行時和'enum'不存在。它只是在編譯時轉換爲整數。你爲什麼不改變你的問題,因此它涵蓋了你正試圖解決的問題,而不是問一個可能的解決方案。 – rmaddy

+0

因爲這是一個預處理器指令,所以無法使用#ifdef。在編譯時評估。 –

回答

2

您不能在運行時執行此操作。另外#ifdef是一個預處理指令,它在編譯過程中被處理。 您可以改爲使用數組,並在運行時添加和刪除項目。所以你必須像

NSMutableArray *arr = @[@"Section 1", @"Section 2", @"Section 3"]; 

如果你得到的響應是不需要第一部分,那麼你可以調用

[arr removeObjectAtIndex:0]; 
+1

只有第一個元素有索引0 :-) –

+0

哎呀,謝謝你的評論:) – igoris

0

確定。我已經通過將MyListSectionType1移動到枚舉列表中的最後,然後在 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView有條件地返回MyListSectionTypeMax - 1來解決此問題。