滚动时 UITableView 崩溃
我正在开发一个小型文本编辑器,我开始使用表视图模板,添加了用于在其上列出应用程序文档目录的内容的代码,一切都很好,我将一些文件复制到该目录,表视图显示了它们,但是当我开始滚动时,它崩溃了。我认为有些东西在不必要的时候就被释放了。这里我留下了代码和崩溃日志:)
RootViewController.m
NSFileManager *directoryContent;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self listDumpFiles];
}
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [directoryContent count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.text = [directoryContent objectAtIndex:indexPath.row];
return cell;
}
- (void)listDumpFiles {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: documentsDirectory];
NSLog(@"%@", documentsDirectory);
return;
}
崩溃日志
2011-02-03 17:38:16.516 uNotes[15352:40b] /Users/pablo/Library/Application Support/iPhone Simulator/4.2/Applications/DF298F31-5723-4A1E-9EAA-3353C34BDCB2/Documents
2011-02-03 17:38:17.412 uNotes[15352:40b] -[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x9b243f0
2011-02-03 17:38:17.413 uNotes[15352:40b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x9b243f0'
*** Call stack at first throw:
(
0 CoreFoundation 0x00da7be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00efc5c2 objc_exception_throw + 47
2 CoreFoundation 0x00da96fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d19366 ___forwarding___ + 966
4 CoreFoundation 0x00d18f22 _CF_forwarding_prep_0 + 50
5 uNotes 0x0000238c -[RootViewController tableView:cellForRowAtIndexPath:] + 216
6 UIKit 0x003247fa -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634
7 UIKit 0x0031a77f -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
8 UIKit 0x0032f450 -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
9 UIKit 0x00327538 -[UITableView layoutSubviews] + 242
10 QuartzCore 0x01c65451 -[CALayer layoutSublayers] + 181
11 QuartzCore 0x01c6517c CALayerLayoutIfNeeded + 220
12 QuartzCore 0x01c5e37c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
13 QuartzCore 0x01c5e0d0 _ZN2CA11Transaction6commitEv + 292
14 QuartzCore 0x01c8e7d5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
15 CoreFoundation 0x00d88fbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
16 CoreFoundation 0x00d1e0e7 __CFRunLoopDoObservers + 295
17 CoreFoundation 0x00ce6bd7 __CFRunLoopRun + 1575
18 CoreFoundation 0x00ce6240 CFRunLoopRunSpecific + 208
19 CoreFoundation 0x00ce6161 CFRunLoopRunInMode + 97
20 GraphicsServices 0x016dc268 GSEventRunModal + 217
21 GraphicsServices 0x016dc32d GSEventRun + 115
22 UIKit 0x002bf42e UIApplicationMain + 1160
23 uNotes 0x00001fc0 main + 102
24 uNotes 0x00001f51 start + 53
)
terminate called after throwing an instance of 'NSException'
Program received signal: “SIGABRT”.
(gdb)
I'm developing a little text editor and I started using the Table View template, added code for listing on it the contents of the app Documents directory, and all is OK, I copied some files to the directory and the table view shows them, but when I start scrolling, it crashes. I think that something is being released when it doesn't have to. Here I left the code and the crash log :)
RootViewController.m
NSFileManager *directoryContent;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self listDumpFiles];
}
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [directoryContent count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.text = [directoryContent objectAtIndex:indexPath.row];
return cell;
}
- (void)listDumpFiles {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: documentsDirectory];
NSLog(@"%@", documentsDirectory);
return;
}
Crash log
2011-02-03 17:38:16.516 uNotes[15352:40b] /Users/pablo/Library/Application Support/iPhone Simulator/4.2/Applications/DF298F31-5723-4A1E-9EAA-3353C34BDCB2/Documents
2011-02-03 17:38:17.412 uNotes[15352:40b] -[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x9b243f0
2011-02-03 17:38:17.413 uNotes[15352:40b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x9b243f0'
*** Call stack at first throw:
(
0 CoreFoundation 0x00da7be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00efc5c2 objc_exception_throw + 47
2 CoreFoundation 0x00da96fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d19366 ___forwarding___ + 966
4 CoreFoundation 0x00d18f22 _CF_forwarding_prep_0 + 50
5 uNotes 0x0000238c -[RootViewController tableView:cellForRowAtIndexPath:] + 216
6 UIKit 0x003247fa -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634
7 UIKit 0x0031a77f -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
8 UIKit 0x0032f450 -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
9 UIKit 0x00327538 -[UITableView layoutSubviews] + 242
10 QuartzCore 0x01c65451 -[CALayer layoutSublayers] + 181
11 QuartzCore 0x01c6517c CALayerLayoutIfNeeded + 220
12 QuartzCore 0x01c5e37c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
13 QuartzCore 0x01c5e0d0 _ZN2CA11Transaction6commitEv + 292
14 QuartzCore 0x01c8e7d5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
15 CoreFoundation 0x00d88fbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
16 CoreFoundation 0x00d1e0e7 __CFRunLoopDoObservers + 295
17 CoreFoundation 0x00ce6bd7 __CFRunLoopRun + 1575
18 CoreFoundation 0x00ce6240 CFRunLoopRunSpecific + 208
19 CoreFoundation 0x00ce6161 CFRunLoopRunInMode + 97
20 GraphicsServices 0x016dc268 GSEventRunModal + 217
21 GraphicsServices 0x016dc32d GSEventRun + 115
22 UIKit 0x002bf42e UIApplicationMain + 1160
23 uNotes 0x00001fc0 main + 102
24 uNotes 0x00001f51 start + 53
)
terminate called after throwing an instance of 'NSException'
Program received signal: “SIGABRT”.
(gdb)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据堆栈跟踪,您尝试在字符串上调用
objectAtIndex:
。可能是内存管理问题。 启用僵尸以获取有关正在调用的对象的更多信息。更新
这可能是这一行
directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: DocumentsDirectory];
我认为
directoryContentsAtPath:
返回一个自动释放的对象。如果您在标头中将directoryContent
设置为@property(nonatomic, keep)
,则可以使用self.directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath 进行设置:documentsDirectory];
并且它会自动保留NSFileManager
返回的对象,否则你可以调用directoryContent = [[[NSFileManager defaultManager]directoryContentsAtPath:documentsDirectory]retain]; 但一定要在不再需要该对象时释放该对象。
According to the stack trace you are attempting to call
objectAtIndex:
on a String. Probably a memory management issue. Enable zombies to get more information about the object it is being called on.UPDATE
It is probably this line
directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: documentsDirectory];
I think
directoryContentsAtPath:
returns an autoreleased object. If you havedirectoryContent
set up as a@property(nonatomic, retain)
in your header you can set it usingself.directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: documentsDirectory];
and it will automatically retain the object returned byNSFileManager
, otherwise you could calldirectoryContent = [[[NSFileManager defaultManager] directoryContentsAtPath: documentsDirectory] retain];
but be sure to release that object when you no longer need it.