ReactiveCocoaDemo icon indicating copy to clipboard operation
ReactiveCocoaDemo copied to clipboard

Add城市后,Controller中多次添加了cell

Open ParallelWorld opened this issue 8 years ago • 1 comments

GeoCityViewController.m文件

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // 回调的处理
    // 此处应该加上self弱引用
    [[self rac_signalForSelector:@selector(didSaveDataCallback:) fromProtocol:@protocol(SaveDataCallBack)] subscribeNext:^(RACTuple *tuple) {
        City *newCity = tuple.first;
        [self.viewModel.cities insertObject:newCity atIndex:0];
        [self.geoTbl reloadData];
    }];

    // 再传递viewDelegate给新页面
    AddCityViewController *addController = (AddCityViewController *)[segue destinationViewController];
    addController.delegate = self;
}

改为

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // 回调的处理

    @weakify(self);
    [[self rac_signalForSelector:@selector(didSaveDataCallback:) fromProtocol:@protocol(SaveDataCallBack)] subscribeNext:^(RACTuple *tuple) {
        @strongify(self);
        City *newCity = tuple.first;
        [self.viewModel.cities insertObject:newCity atIndex:0];
        [self.geoTbl reloadData];
    }];

    // 再传递viewDelegate给新页面
    AddCityViewController *addController = (AddCityViewController *)[segue destinationViewController];
    addController.delegate = self;
}

ParallelWorld avatar Apr 06 '16 14:04 ParallelWorld

@parallelWorld 对协议信号的订阅应该可以拿出去写吧,订阅一次就好了,写在prepareForSegue中不会每次都调用这个信号的订阅吗?

ShowHandAce avatar Oct 17 '17 03:10 ShowHandAce