简体   繁体   中英

Reloading UITableView

I've read some of the other posts but haven't found the right solution that seems to work in my case. I've got a UITableViewController class that is instantiated from the Main App Delegate and it controls other views that get pushed into the UINavigationController instance created from the App Delegate.

On the UINavigationToolBar I have a UISegmentedControl button that is split in two pieces, Factory Loads and User Loads. I have an action method attached to the SegmentedControl to detect when the button is pressed and that method is inside the UIViewController Class. My goal is to have the view reload with the contents of a new dictionary that is constructed from a plist file in the app bundle. I have the dictionary loaded, and the keys to be displayed set up in the respective data formats, but when I try to reload the UITableView, it crashes.

This is the last code I tried to get to work inside my action method, but it still doesn't function as intended.

- (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];
}
}

I'm calling [[self tableView] reloadData] in an attempt to retrieve the actual table contained within the UIViewController to try to get the table to reload, but no luck. Any help is appreciated, and if more code is needed please ask. Thanks!

Andrew

------- Edit ----------- Here is the new code in reference to Chip's ideas. The issue is still not resolved and the app still crashes.

- (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];
}

I would pre-load the dictionaries and hold them as ivars in the controller class.

I am betting that the loading of the dictionary contents is not complete, so you are in effect calling reloadData while the data is changing which is causing your crash.

Also, the read is an expensive option and you are loading each time the segementControl changes state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM