[英]UITableView crashes the application when scrolled
我無法找到滾動時UITableView崩潰的問題的解決方案。 我正在使用以下來自Apple文檔的代碼:
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
除其他事項外,這里的問題可能是與您預期的內存管理相關的。 我相信問題在於, 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];
}
從堆棧跟蹤中得出的結果是來自類別數組上調用的objectForKey方法的。
據我了解,NSArray沒有方法'objectForKey'。 您可能需要NSDictionary。
該堆棧跟蹤表明您正在嘗試在不具有該方法的類上調用該方法。 具體來說,您將在NSArray
實例上調用objectForKey
。 您確定categoryArray
是NSDictionary
的實例,而不是NSArray
的實例嗎?
看起來在使用categoryArray
數組之前,該對象已被釋放。 然后,該內存地址被重用於另一個對象,恰好是__NSArrayM
。
您可以在viewDidLoad
分配變量。
categoryArray = [myapp getCategory];
不確定沒有看到getCategory
的內容,但是我懷疑返回值是自動釋放的。 因此,您需要獲取對象的所有權,以使其在當前方法的結尾處徘徊。
您應該使用setter:
[self setCategoryArray:[myapp getCategory]];
或保留返回值:
categoryArray = [[myapp getCategory] retain];
另外,您應該重命名該getCategory
方法。 按照約定,以get
開頭的方法具有特定的含義:它們將指針指向指針作為參數,並將值放入該位置以供調用者使用。 參見例如-[NSArray getObjects:range:]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.