Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用scrollViewDidScroll执行错误访问_Ios_Iphone_Cocoa Touch_Uiscrollview_Uikit - Fatal编程技术网

Ios 使用scrollViewDidScroll执行错误访问

Ios 使用scrollViewDidScroll执行错误访问,ios,iphone,cocoa-touch,uiscrollview,uikit,Ios,Iphone,Cocoa Touch,Uiscrollview,Uikit,我一定错过了一些明显的东西,因为我有一个非常简单的应用程序,它会生成一个EXC_BAD_访问错误 我在导航控制器中有一个第一视图控制器。 从那里,我推送第二个视图控制器和UITableView。 在第二个视图控制器中,我实现了scrollViewDidScroll委托方法 当我向下滚动到表的底部,然后返回到第一个视图控制器时,会出现EXC\u BAD\u访问错误 以下是第一个视图控制器的代码: @implementation ViewController - (void)viewDidLoad

我一定错过了一些明显的东西,因为我有一个非常简单的应用程序,它会生成一个EXC_BAD_访问错误

我在导航控制器中有一个第一视图控制器。 从那里,我推送第二个视图控制器和UITableView。 在第二个视图控制器中,我实现了scrollViewDidScroll委托方法

当我向下滚动到表的底部,然后返回到第一个视图控制器时,会出现EXC\u BAD\u访问错误

以下是第一个视图控制器的代码:

@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
-(void)dealloc
{
    self.tableView.dataSource = nil;
    self.tableView.delegate = nil;
}
下面是第二个视图控制器的代码

@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
@interface ViewController2()
@属性(非原子,强)UITableView*tableView;
@结束
@实现视图控制器2
-(无效)viewDidLoad{
[超级视图下载];
self.tableView=[[UITableView alloc]initWithFrame:self.view.bounds];
self.tableView.autoresizingMask=uiviewsautoresizingflexiblewidth | uiviewsautoresizingflexiblewhight;
self.tableView.dataSource=self;
self.tableView.delegate=self;
[self.view addSubview:self.tableView];
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回20;
}
-(无效)scrollViewDidScroll:(UIScrollView*)scrollView{
NSLog(@“滚动”);
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
NSString*identifer=@“identifer”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:Identifier];
如果(!单元格){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:标识符];
}
cell.textLabel.text=[NSString stringWithFormat:@“cell%d”,indexPath.row];
返回单元;
}
@结束
当我注释掉scrollViewDidScroll方法时,或者如果我在返回到第一个视图控制器之前没有滚动到表的末尾,则不会生成错误

我使用的是XCode 6(带iOS SDK 8),并在iOS 7和iOS 8模拟器和物理设备上进行了测试


这有意义吗?

您需要在第二个视图控制器的dealloc方法中将UITableView的数据源和委托属性设置为nil。UITableView正在向已解除分配的委托发送一些消息

因此,在第二个视图控制器中,类似于以下内容:

@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
-(void)dealloc
{
    self.tableView.dataSource = nil;
    self.tableView.delegate = nil;
}

我只是不明白为什么苹果没有将这些属性更新为
weak
?!