所以我有這個非常基本的ipad視圖控制器,我正在做一些測試與多個UITableViews在同一視圖。我在當時我選擇了一排,它會拋出一個EXC_BAD_ACCESS的問題,所以我把堆棧記錄,發現這個有趣的UITableView數據源行爲
*** -[VCTables tableView:didSelectRowAtIndexPath:]: message sent to deallocated instance 0x4c0ad40
然後我就開始看我的代碼和無法弄清楚的生活我是什麼導致它。我有我的視圖控制器上的UITableViewDataSource和UITableViewDelegate並具有所有適當的代碼來處理索引選擇行。它正確地繪製了表格,它似乎並沒有將自己作爲數據源。
我試過在代碼中構建UITableViews,並且在界面構建器中也是這樣。在卸載它並重新啓動後,我重新下載並安裝了XCode 3.2.3 SDK 4.0.2。我不能爲了我的生活看到我失去了什麼,但在做了之前,我現在確信(這是一個代碼問題,而不是IDE),我只是無法打開我的眼睛足以看到代碼問題。
此外,這也只發生在一張桌子上。並與2個表,它發生,無論我選擇哪個表
這裏是一些代碼:
VCTables.h
#import <UIKit/UIKit.h>
@interface VCTables : UIViewController<UITableViewDelegate,UITableViewDataSource> {
UITableView *table1;
UITableView *table2;
}
@end
VCTables.m
#import "VCTables.h"
@implementation VCTables
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 7;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [[NSString alloc] initWithString:@"yourCell"];
if(tableView.tag == 2000){
[CellIdentifier release];
CellIdentifier = [[NSString alloc] initWithString:@"myCell"];
}
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(tableView.tag == 2000){
[cell.textLabel setText:[NSString stringWithFormat:@"%d",indexPath.row]];
}else{
[cell.textLabel setText:[NSString stringWithFormat:@"%d%d",indexPath.row,indexPath.section]];
}
[CellIdentifier release];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
NSLog(@"selected");
}
- (void)viewDidLoad {
[super viewDidLoad];
table1 = [[UITableView alloc] initWithFrame:CGRectMake(20, 73, 320, 480) style:UITableViewStylePlain];
table1.delegate=self;
table1.dataSource=self;
table1.tag=2000;
[self.view addSubview:table1];
table2 = [[UITableView alloc] initWithFrame:CGRectMake(483, 73, 320, 480) style:UITableViewStylePlain];
table2.delegate=self;
table2.dataSource=self;
table2.tag=2000;
[self.view addSubview:table2];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[super dealloc];
}
@end
我不是肯定它比這更基本。請告訴我這個如此痛苦明顯的菜鳥錯誤,我必須停止在這裏發帖。在此先感謝
嘗試在創建表格時保留表格 - 正常的EXC_BAD_ACCESS表示某些東西在您使用它時自動釋放/遺忘。 :)你還需要記住在 - (void)dealloc中釋放你的表。 – 2010-08-27 15:19:50
是的,這只是用於SO目的的修剪代碼。問題在於VC本身被釋放了一層以上,所以實際上沒有VCTables對象成爲視圖中引發表的委託。 – AtomRiot 2010-08-27 15:24:47