【发布时间】:2011-11-06 04:52:21
【问题描述】:
我在 iOS5 中的 tableview 遇到了这个问题,它给了我错误,请注意它适用于较旧的 sdk。 这是相关代码: h 文件 #导入
@interface WordListTableViewController : UITableViewController {
//NSMutableDictionary *words;
//NSArray *sections;
}
@end
m 文件 #import "WordListTableViewController.h" #import "DefinitionViewController.h"
@interface WordListTableViewController()
@property (nonatomic, strong) NSMutableDictionary *words;
@property (nonatomic, strong) NSArray *sections;
@end
@implementation WordListTableViewController
@synthesize words, sections;
- (NSMutableDictionary *) words
{
if(!words){
NSURL *wordsURL = [NSURL URLWithString:@"http://localhost/Vocabulous.txt"];
words = [NSMutableDictionary dictionaryWithContentsOfURL:wordsURL] ;
}
return words;
}
- (NSArray *) sections
{
if(!sections)
{
sections = [[self.words allKeys] sortedArrayUsingSelector:@selector(compare:)] ;
}
return sections;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"WordListTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
//cell.textLabel.text = cell.textLabel.text = [self wordAtIndexPath:indexPath];
cell.textLabel.text = @"Test";
return cell;
}
h 应用程序委托文件 #导入
@interface VocabAppDelegate : UIResponder <UIApplicationDelegate>
{
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *nav;
@end
m 带有相关代码的 App Delegate 文件: #import "VocabAppDelegate.h" #import "WordListTableViewController.h"
@implementation VocabAppDelegate
@synthesize window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
WordListTableViewController *wltvc = [[WordListTableViewController alloc] init];
self.nav = [[UINavigationController alloc] initWithRootViewController: wltvc ];
self.window.rootViewController = self.nav;
[self.window makeKeyAndVisible];
return YES;
}
错误如下:EXEC_BAD_ACCESS 就像它只设置可见单元格,当我开始向下滚动寻找不可见的单元格时,就好像它们无法显示一样。
国际泳联更新: 按照 mattyhoe 的建议,我用静态文本替换了 wordAtIndexPath 方法,并按照另外两个边注加上我使用委托的方式,它可以工作!!!
谢谢
【问题讨论】:
-
你做错了几件事。首先,
addSubview:位实际上是一种旧的做事方式,除非您支持 4.0 之前的应用程序(启用 ARC 时无法实现),否则您希望在 @987654327 上实际设置rootViewController属性@。我建议在您的应用程序委托中为您的导航控制器创建一个@property,并使用initWithRootViewController:分配它。所以:self.nav = [[UINavigationController alloc] initWithRootViewController: [[WordListTableViewController alloc] init]];然后继续添加导航作为窗口的 RVC。 -
有关如何执行此操作的示例,请创建一个新项目并选择主从应用程序。查看应用委托,了解如何正确设置视图控制器。
-
发布崩溃的堆栈跟踪以及导致崩溃的任何其他记录的消息。
-
@robmayoff 来自 iOS 视图编程指南:“要在窗口中安装视图,请使用 addSubview: 方法....代替前面的代码,您也可以在 nib 文件中配置窗口的 rootViewController 属性。"
-
好吧,我不得不说,在启用 iOS5 和 ARC 的实践中,它不起作用。 rootViewController 就像 mayttyhoe 所说的那样,我的代码已经更新
标签: objective-c uitableview ios5 automatic-ref-counting