2011-06-21 211 views
0

我讀過一些其他帖子,但還沒有找到正確的解決方案,似乎在我的情況下工作。我有一個從主應用程序委託實例化的UITableViewController類,它控制其他視圖,這些視圖被推入從App Delegate創建的UINavigationController實例中。重新加載UITableView

在UINavigationToolBar上,我有一個UISegmentedControl按鈕,它分爲兩部分:Factory Loads和User Loads。我有一個附加到SegmentedControl的動作方法來檢測按鈕何時被按下,並且該方法在UIViewController類中。我的目標是讓視圖重新加載由應用程序包中的plist文件構建的新字典的內容。我加載了字典,並以相應的數據格式設置顯示的鍵,但是當我嘗試重新加載UITableView時,它崩潰了。

這是我嘗試在我的操作方法中工作的最後一個代碼,但它仍然無法按預期運行。

- (void) action:(id)sender { 

UISegmentedControl *segment = (UISegmentedControl *) sender; 
if ([segment selectedSegmentIndex] == 1) 
{ 
    NSLog(@"User Load Selected"); 

    NSString *userLoadFilePath = [[NSBundle mainBundle] pathForResource:@"UserLoads" ofType:@"plist"]; 
    NSDictionary *userLoadDictionary = [[NSDictionary alloc] initWithContentsOfFile:userLoadFilePath]; 
    NSArray *allKeys = [userLoadDictionary allKeys]; 
    keys = allKeys; 
    dictionary = userLoadDictionary; 

    [[self tableView] reloadData]; 
} 

else if ([segment selectedSegmentIndex] == 0) 
{ 
    NSLog(@"Factory Load Selected"); 

    NSString *factoryLoadFilePath = [[NSBundle mainBundle] pathForResource:@"AlliantPowder" ofType:@"plist"]; 
    NSDictionary *factoryLoadDictionary = [[NSDictionary alloc] initWithContentsOfFile:factoryLoadFilePath]; 
    NSArray *allKeys = [factoryLoadDictionary allKeys]; 
    keys = allKeys; 

    [[self tableView] reloadData]; 
} 
} 

我試圖檢索的UIViewController內包含嘗試讓表重新加載實際的表,但沒有運氣調用[[self tableView] reloadData]。任何幫助表示讚賞,如果需要更多代碼請詢問。謝謝!

安德魯

-------編輯----------- 這裏是參照芯片的思路,新的代碼。這個問題仍然沒有解決,應用程序仍然崩潰。

- (void) action:(id)sender { 

// Create an instance of UISegmentedControl and set it as the sending event. 
UISegmentedControl *segment = (UISegmentedControl *) sender; 

// Detect which button was pressed and load a new dictionary appropriately 
// Checking if userLoads was selected 
if ([segment selectedSegmentIndex] == 1) 
{ 
    NSLog(@"User Load Selected"); 

    keys = userKeys; 
    [[self tableView] reloadData]; 
} 

// Checking if factoryLoads was selected 
else if ([segment selectedSegmentIndex] == 0) 
{ 
    NSLog(@"Factory Load Selected"); 

    keys = [dictionary allKeys]; 
    [[self tableView] reloadData]; 

} 

[segment release]; 
} 
+0

你在哪裏實現' - (void)action:(id)sender {'? –

+0

操作方法的實現在類RootViewController.m中,UISegmentedController也是在這裏創建和運行的。這個類是一個UITableViewController,它擴展了UITableViewDataSource,UITableViewDelegate類,並且具有用於創建其UITableView的推薦方法。這就是爲什麼我調用'[[self tableView] reloadData]'。我想我可以提取tableView,然後用新的鍵數組重新加載它。單元格創建很簡單,只需根據數組中的值設置標題。 – Andrew

+0

你可能應該分配給屬性而不是ivars; ' - [NSDictionary allKeys]'可能是autoreleased,從而導致你的崩潰(也有一堆泄漏)。 –

回答

0

我會預加載字典,並將它們保存爲控制器類中的ivars。

我打賭,字典內容的加載不完整,所以你實際上調用reloadData而數據正在改變,這是導致你的崩潰。

此外,讀取是一個昂貴的選項,每次segementControl更改狀態時都要加載。

+0

謝謝,我會嘗試這種方法並加載字典,當應用程序啓動時加載原始字典。我會盡力跟上我的內存泄漏。就像我在上面的評論中所說的那樣,我更關注的是在這個時候製作功能性代碼而不是優化代碼。謝謝! – Andrew

+0

因此,我按照您的建議重新添加了代碼以添加ivars,並在應用程序的早期設置它們,以便我們可以切換各個鍵以顯示新內容。使用'NSLog()'我已經能夠驗證它從文件中加載數據,但是在嘗試重新加載'UITableView'時它仍然崩潰。我已更新帖子以添加新代碼。 – Andrew

+0

你可以發佈崩潰堆棧跟蹤和/或崩潰發生的方法中的代碼(即它是在你的操作方法還是在「cellForIndexPath ...」方法?) –