[英]Error EXC_BAD_ACCESS with scrollViewDidScroll
我必須缺少明顯的東西,因為我有一個非常簡單的應用程序,會生成EXC_BAD_ACCESS錯誤。
我在導航控制器內部有第一個視圖控制器。 從那里,我用UITableView推送了另一個視圖控制器。 在第二個視圖控制器中,我實現了scrollViewDidScroll委托方法。
當我向下滾動到表的底部,然后返回到第一個視圖控制器時,出現了EXC_BAD_ACCESS錯誤。
這是第一個視圖控制器的代碼:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 50)];
button.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[button setTitle:@"Push tableView" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void) buttonClick {
[self.navigationController pushViewController:[ViewController2 new] animated:YES];
}
@end
這是第二個視圖控制器的代碼
@interface ViewController2 () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView* tableView;
@end
@implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(@"scroll");
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* identifer = @"identifer";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifer];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
}
cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row];
return cell;
}
@end
當我注釋掉scrollViewDidScroll方法時,或者如果在返回第一個視圖控制器之前直到表的末尾才滾動,才不會生成該錯誤。
我正在使用XCode 6(帶有iOS SDK 8),並且已經在iOS 7和iOS 8模擬器以及物理設備上進行了測試。
這有道理嗎?
您需要在第二個視圖控制器的dealloc方法中將UITableView的數據源和委托屬性設置為nil。 UITableView已被釋放后,正在向代理發送一些消息。
因此,在您的第二個視圖控制器中是這樣的:
-(void)dealloc
{
self.tableView.dataSource = nil;
self.tableView.delegate = nil;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.