2011-06-21 214 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 ...”方法?) –