【问题标题】:App crash when UITableView Scrolls upUITableView 向上滚动时应用程序崩溃
【发布时间】:2011-09-21 21:57:12
【问题描述】:

我想知道为什么当我向上滚动 UITableView 时我的应用程序崩溃了。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    NSArray *arrayForNames=[[NSArray alloc]initWithArray:[objContentManager getDuasNamesByGroupName:[arrayDuaGroups objectAtIndex:indexPath.section]]]; 


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
// implementation on cell    
}

当我向上滚动时,程序在UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 处崩溃

我看到了教程,我在那里找不到任何不同的实现,它向下滚动非常流畅,但是当我向上滚动时它立即崩溃,我不知道我在哪里做错了

> 2011-06-24 15:07:10.976
> Tasbeeh[503:207] *** Terminating app
> due to uncaught exception
> 'NSRangeException', reason: '***
> -[NSArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
> *** Call stack at first throw: (  0   CoreFoundation                     
> 0x02553919 __exceptionPreprocess + 185
>   1   libobjc.A.dylib                  
> 0x023685de objc_exception_throw + 47
>   2   CoreFoundation                   
> 0x0254958c -[__NSArrayI
> objectAtIndex:] + 236     3   Tasbeeh    
> 0x00002922 -[ClassDuaTableCategory
> tableView:cellForRowAtIndexPath:] +
> 553   4   UIKit                        
> 0x00326a3f
> -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:]
> + 619     5   UIKit                               0x0031cad2
> -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75     6   UIKit                            
> 0x00331337
> -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1348    7   UIKit                              
> 0x003294bc -[UITableView
> layoutSubviews] + 242     8   QuartzCore 
> 0x040e30d5 -[CALayer layoutSublayers]
> + 177     9   QuartzCore                          0x040e2e05 CALayerLayoutIfNeeded + 220
>   10  QuartzCore                       
> 0x040e264c
> _ZN2CA7Context18commit_transactionEPNS_11TransactionE
> + 302     11  QuartzCore                          0x040e22b0
> _ZN2CA11Transaction6commitEv + 292    12  QuartzCore                         
> 0x040e9f5b
> _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv
> + 99  13  CoreFoundation                      0x02534d1b
> __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__
> + 27  14  CoreFoundation                      0x024c9987 __CFRunLoopDoObservers +
> 295   15  CoreFoundation               
> 0x02492c17 __CFRunLoopRun + 1575  16 
> CoreFoundation                     
> 0x02492280 CFRunLoopRunSpecific + 208
>   17  CoreFoundation                   
> 0x024921a1 CFRunLoopRunInMode + 97    18
> GraphicsServices                   
> 0x02cb82c8 GSEventRunModal + 217  19 
> GraphicsServices                   
> 0x02cb838d GSEventRun + 115   20  UIKit
> 0x002c4b58 UIApplicationMain + 1160
>   21  Tasbeeh                          
> 0x00002024 main + 102     22  Tasbeeh    
> 0x00001fb5 start + 53 ) terminate
> called after throwing an instance of
> 'NSException' Program received signal:
> “SIGABRT”.

请给我建议。

谢谢。

【问题讨论】:

  • crash log plz 和 implementation cell 也在这里提供..
  • 我已经贴好了,请看一下
  • 您可能想要添加getDuasNamesByGroupName 方法,以及显示您如何填充和更改arrayDuaGroups 的代码。

标签: iphone objective-c xcode ios4


【解决方案1】:

请检查您的 tableview 控制器的 nib 文件。

您的视图控制器下应该有一个“表格视图”。

点击查看表格视图的属性检查器。

检查“内容”看它是否被分配了“动态原型”或“静态单元格”?

从“静态单元”更改为“动态原型”也许可以解决您的问题。

【讨论】:

  • 这对我的 tableview 有同样的问题没有帮助。
【解决方案2】:

indexPath 是否有可能指向指向数组arrayDuaGroups 的无效索引的值 ?

【讨论】:

  • 这就是重点,但为什么它会在UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 崩溃,为什么会这样
  • 观察异常堆栈,似乎错误在 0x0254958c -[__NSArrayI objectAtIndex:] + 236 3 而不是 [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
【解决方案3】:

尝试评论 cell = nil if 并且只让 cell = part ,如果这是我认为的那样,它将修复它

 // if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//    }

编辑: 查看日志后- 问题是数组作为数据源,当它尝试重建单元格时,它在您的数组(arrayDuaGroups)中找不到任何东西,它是空的,在类接口中声明您的数组并使用保留属性检查是否arrayDuaGroups 为空

【讨论】:

  • 谢谢 Radu 我猜我有一个逻辑问题导致了这个错误
【解决方案4】:

似乎数组越界异常。你的数组是问题所在。您不应该将数组分配到 cellForRowAtIndexPath 方法中。在 viewDidLoad 方法中填充数据,然后使用该数组在单元格中显示数据。

【讨论】:

  • 感谢您的帮助,但我认为我在管理组中的行时存在逻辑问题
  • 根据您的崩溃日志,您的问题是由于数组越界异常,indexPath 将指向一个指向无效索引的值。
【解决方案5】:

尝试使用下面的

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell_%d_%d",indexPath.section,indexPath.row];

EDITED:

您对 NSArray 有疑问,您正在尝试访问数组中不存在的元素,

您的arrayDuaGroups 数组在索引indexPath.section 处没有任何对象。

在获取对象之前检查arrayDuaGroups数组的内容和长度。

[arrayDuaGroups objectAtIndex:indexPath.section]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 2018-01-16
    • 2011-07-10
    相关资源
    最近更新 更多