我讀過一些其他帖子,但還沒有找到正確的解決方案,似乎在我的情況下工作。我有一個從主應用程序委託實例化的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];
}
你在哪裏實現' - (void)action:(id)sender {'? –
操作方法的實現在類RootViewController.m中,UISegmentedController也是在這裏創建和運行的。這個類是一個UITableViewController,它擴展了UITableViewDataSource,UITableViewDelegate類,並且具有用於創建其UITableView的推薦方法。這就是爲什麼我調用'[[self tableView] reloadData]'。我想我可以提取tableView,然後用新的鍵數組重新加載它。單元格創建很簡單,只需根據數組中的值設置標題。 – Andrew
你可能應該分配給屬性而不是ivars; ' - [NSDictionary allKeys]'可能是autoreleased,從而導致你的崩潰(也有一堆泄漏)。 –