iOS UITableView 滚动时崩溃
我在这里遇到了一个奇怪的问题。我正在开发一个 iOS 应用程序(特别是针对 iPad),并且有时会使用 UITableView 来显示事物列表。
现在,当我在视图的边界内滚动(不在第一个元素之上,也不在最后一个元素之下)时,它可以正常工作。但是,当我滚动得更远时,它会剧烈崩溃,除了以下消息之外没有其他消息:
- EXC_BAD_ACCESS 当我向下滚动到最后一个元素时
- SIGABRT 当我向上滚动时带有回溯比我第一次
在 Google 上看到的要多,似乎我释放了太多对象,但我不知道是哪些对象。
我还尝试在仪器内运行该应用程序,但每次运行该应用程序时,仪器窗口都会冻结,迫使我手动杀死它...当然我没有得到任何结果...
这里有一些相关代码:
/*
Returns the cells of the table view
*/
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Create a new cell view
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = [newestModules objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"Background-Texture-Dark-Small.png"]];
cell.imageView.image = [UIImage imageNamed:@"Icon-Maths.png"];
UIView *v = [[[UIView alloc] initWithFrame:cell.frame] autorelease];
// Set view background color
v.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background-Texture-Dark-Small.png"]];
// This view will be activated when the cell is selected
cell.selectedBackgroundView = v;
return cell;
}
编辑:UITableView加载和卸载方法:
- (void)viewDidLoad
{
[super viewDidLoad];
// Transparent background
self.tableView.backgroundView = nil;
// Generate list of newest modules. Will later look for them on the internet, but for now we only add some test examples.
newestModules = [[NSMutableArray alloc] initWithObjects:@"Test 1", @"Test 2", @"Test 3", @"Test 4", @"Test 5", nil];
}
- (void)viewDidUnload
{
[newestModules release];
[super viewDidUnload];
}
I ran into a bizarre issue here. I'm developping an iOS app (for iPad, specifically), and I'm using an UITableView at some point to display a list of things.
Now, when I scroll inside the bounds of the view (not above first element, and not below the last), it works okay. However, it just crashes violently when I scroll further than that, with no other messages than :
- EXC_BAD_ACCESS when I scroll down to the last element
- SIGABRT with a backtrace when I scroll upper than the first
I looked on Google, and it seems like I'm releasing some objects too much, but I can't figure out which ones.
I also tried running the app inside the Instruments, but the Instruments window just freezes each time I run the app, forcing me to kill it by hand... And of course I get no results...
Here a bit of the related code :
/*
Returns the cells of the table view
*/
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Create a new cell view
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = [newestModules objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"Background-Texture-Dark-Small.png"]];
cell.imageView.image = [UIImage imageNamed:@"Icon-Maths.png"];
UIView *v = [[[UIView alloc] initWithFrame:cell.frame] autorelease];
// Set view background color
v.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background-Texture-Dark-Small.png"]];
// This view will be activated when the cell is selected
cell.selectedBackgroundView = v;
return cell;
}
EDIT: UITableView Load and Unload methods :
- (void)viewDidLoad
{
[super viewDidLoad];
// Transparent background
self.tableView.backgroundView = nil;
// Generate list of newest modules. Will later look for them on the internet, but for now we only add some test examples.
newestModules = [[NSMutableArray alloc] initWithObjects:@"Test 1", @"Test 2", @"Test 3", @"Test 4", @"Test 5", nil];
}
- (void)viewDidUnload
{
[newestModules release];
[super viewDidUnload];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来,当您在 Interface Builder 中添加一个对象(例如新控制器)时,默认情况下它会自动释放。
如果您不将其与类内的保留属性链接,它会在初始化后立即释放,从而导致可怕的EXC_BAD_ACCESS错误。
It seems that, when you add an object, like a new controller, inside the Interface Builder, it is auto-released by default.
If you don't link it with a retained property inside a class, it gets released right after its initialization, causing the dreaded EXC_BAD_ACCESS error.
根据经验,边界内滚动和边界滚动之间的最大区别在于,边界内滚动会遍历表格的所有元素,包括页眉和页脚,并且很可能重新计算行数。如果您有页眉和页脚,请尝试在其中放置断点。
关于EXC_BAD_ACCESS,您可以在malloc_error_break中放置一个断点,以希望更多地了解谁没有被正确释放。这是一个符号断点,可以使用断点窗口上的“+”按钮来定义。
From experience, the big difference between the scrolling within bounds and at bounds is that at bounds it goes through all elements of the tables, including header and footer, and most likely recalculating the number of rows. If you have a header and footer try to put breakpoints there.
Regarding the EXC_BAD_ACCESS you could put a breakpoint in malloc_error_break to hoperfully know more of who is not correctly released. This is a symbolic breakpoint to define with the "+" button on the breakpoints window.