App crash when UITableView Scrolls up

懵懂的女人 提交于 2019-12-22 20:27:07

问题


I wonder why my app crashes when i scroll my UITableView up.

- (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    
}

When i scroll up program crashes at UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

I saw the tutorials and all i can't find any different implementation there, it scrolls down pretty smooth but when i scroll back up it crashes straight away, I not figuring out where m doing wrong

> 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”.

Please suggest me.

Thank you.


回答1:


Is it possible that indexPath goes to a value that points to an invalid index of the array arrayDuaGroups ?




回答2:


Please check the nib file of your tableview controller.

There should be a "table view" under your view controller.

Click to see the attribute inspector of the table view.

Check the "content" to see if it is assigned "Dynamic Prototypes" or "static cell"?

Change from "static cell" to "Dynamic Prototypes" might be able to solve your problem.




回答3:


Try commenting the cell = nil if and let only the cell = part , if it's what i think it is it will fix it

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

EDIT: AFTER viewing the log - The problem is with the array as data source, when it tries to rebuild the cell it does not find anything in your array(arrayDuaGroups), it is empty , declare your array in the class interface with the retain property and check to se if the arrayDuaGroups is empty




回答4:


It seems array out of bounds exception. Your array is the problem. you should not allocate array into cellForRowAtIndexPath method. Fill the data in viewDidLoad method and then use that array to display data in cell.




回答5:


Try with using below

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

EDITED:

You have issue with NSArray , You are trying to access an element from the array which doesn't exist,

Your arrayDuaGroups array does'nt have any object at index indexPath.section.

Check the arrayDuaGroups array content and length before fetching the object.

[arrayDuaGroups objectAtIndex:indexPath.section]



来源:https://stackoverflow.com/questions/6466240/app-crash-when-uitableview-scrolls-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!