2011-11-23 83 views
1

我无法找到UITableView在滚动时崩溃我的应用程序的问题的解决方案。我使用从Apple文件采取了以下代码:UITableView在滚动时崩溃应用程序

NSDictionary *categoryArray; 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg_black.png"]]; 
    self.view.backgroundColor = background; 
    [background release]; 

    MyApp *myapp = [[MyApp alloc ] init]; 
    categoryArray = [myapp getCategory]; 

} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [categoryArray count]; 
} 

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

    // Set up the cell... 
    NSString *cellValue = [categoryArray objectForKey: [NSString stringWithFormat:@"%d", indexPath.row+1]]; 
    cell.textLabel.text = cellValue; 

    return cell; 
} 

凡的NSDictionary categoryArray看起来像这样(从全球单例类检索):

{ 
    1 = Bars; 
    2 = Nightclubs; 
    3 = Societies; 
    4 = Colleges; 
    5 = University; 
} 

我敢肯定它是一些内存分配/再生单元格的种类,但我无法确切地知道我出错的地方。

引发的错误:

2011-11-23 21:29:01.888 WhatsOniPhone[3759:b303] -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x4e7a3f0 
2011-11-23 21:29:01.892 WhatsOniPhone[3759:b303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x4e7a3f0' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x00dde5a9 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x00f32313 objc_exception_throw + 44 
    2 CoreFoundation      0x00de00bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 
    3 CoreFoundation      0x00d4f966 ___forwarding___ + 966 
    4 CoreFoundation      0x00d4f522 _CF_forwarding_prep_0 + 50 
    5 WhatsOniPhone      0x0000340a -[FirstViewController tableView:cellForRowAtIndexPath:] + 314 
    6 UIKit        0x000a5b98 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634 
    7 UIKit        0x0009b4cc -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75 
    8 UIKit        0x000b07f7 -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1348 
    9 UIKit        0x000a890c -[UITableView layoutSubviews] + 242 
    10 QuartzCore       0x016c8a5a -[CALayer layoutSublayers] + 181 
    11 QuartzCore       0x016caddc CALayerLayoutIfNeeded + 220 
    12 QuartzCore       0x016700b4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310 
    13 QuartzCore       0x01671294 _ZN2CA11Transaction6commitEv + 292 
    14 QuartzCore       0x0167146d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99 
    15 CoreFoundation      0x00dbf89b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27 
    16 CoreFoundation      0x00d546e7 __CFRunLoopDoObservers + 295 
    17 CoreFoundation      0x00d1d1d7 __CFRunLoopRun + 1575 
    18 CoreFoundation      0x00d1c840 CFRunLoopRunSpecific + 208 
    19 CoreFoundation      0x00d1c761 CFRunLoopRunInMode + 97 
    20 GraphicsServices     0x010161c4 GSEventRunModal + 217 
    21 GraphicsServices     0x01016289 GSEventRun + 115 
    22 UIKit        0x0003ec93 UIApplicationMain + 1160 
    23 WhatsOniPhone      0x00002759 main + 121 
    24 WhatsOniPhone      0x000026d5 start + 53 
) 
terminate called throwing an exceptionsharedlibrary apply-load-rules all 
+0

请发布回溯。 – Kevin

+0

恩,那个代码不是来自Apple文档。 'categoryArray'是'NSArray'或'NSDictionary'的一个实例。如果它是'NSDictionary',则表示通过将其命名为数组而将事情混淆。此外,您显示的内容就好像它是一个数组,字典由不是整数的对象键入。你如何定义'categoryArray'属性?它是“强”还是“保留”? – XJones

回答

2

这里的问题是,如您所预测的那样,可能存在内存管理问题。我认为问题在于categoryArray正在被释放,并且在内存中由另一个对象替换,在您的异常情况下是NSArray。解决这个问题的办法是保留categoryArray,因为由于某种原因,它不能被视图控制器正确保留。因此,无论您何时指定categoryArray,请执行categoryArray = [... retain]而不是categoryArray = ...。如果您将categoryArray作为属性进行分配,请将该属性声明为@property (nonatomic, retain) NSDictionary * categoryArray

如果您的视图控制器保留的categoryArray所有权,因为它应该如下就必须释放它的dealloc方法:

- (void)dealloc { 
    // if using a property 
    self.categoryArray = nil; 
    // if not using a property 
    [categoryArray release]; 
    ... 
    [super dealloc]; 
} 
+0

干杯队友,工作完美。我知道这会很简单。 – SamRowley

1

从您的堆栈跟踪isue从调用您的类阵列上的objectForKey方法来。

从我的理解NSArray没有方法'objectForKey'。你可能需要一个NSDictionary。

+0

这是一个NSDictionary,只是我愚蠢的命名决定称之为categoryArray。 – SamRowley

+0

@SamRowley不,它不是。检查设置,因为它肯定是一个'NSArray'。 –

+0

@Wayne - 嗯,是的。它使用将JSONString转换为NSDictionary的SBJson库创建。 – SamRowley

1

该堆栈跟踪表明您正试图在没有该方法的类上调用方法。具体而言,您在NSArray的实例上调用objectForKey。您确定categoryArrayNSDictionary的实例,而不是NSArray吗?

+0

是的100%,它会在数据滚动时崩溃时使用数据填充UITableView。 – SamRowley

+0

更改“NSString * cellValue = [categoryArray objectForKey:[NSString stringWithFormat:@”%d“,indexPath.row + 1]];”to NSString * cellValue = @“foo”;并看看是否有效。 –

1

看起来像你之前在categoryArray的对象被释放用它。内存地址然后被重新用于另一个对象,这恰好是__NSArrayM

您分配给您的变量viewDidLoad

categoryArray = [myapp getCategory]; 

这不是没有看到的getCategory内容肯定的,但我怀疑的返回值会被自动释放。因此,您需要获取对象的所有权,以便在当前方法结束时挂起对象。

您应该使用二传手:

[self setCategoryArray:[myapp getCategory]]; 

或保留返回值:

categoryArray = [[myapp getCategory] retain]; 

此外,你应该重新命名getCategory方法。按照惯例,从get开始的方法具有特定的含义:它们将指针指针作为参数,并将值放入该位置以供调用者使用。见,例如-[NSArray getObjects:range:]