RAC用于UITableViewCell复用时的问题

UITableViewCellUICollectionViewCell复用使用RAC的问题,解决复用cell中信号的办法就是在cell里面创建的信号加上takeUntil:cell.rac_prepareForReuseSignal来让cell在每次重用的时候都去
disposable创建的信号(解绑信号)

具体看代码:

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1000;
}

- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell"];

    @weakify(self);
    [RACObserve(cell.textLabel, text) subscribeNext:^(id x) {
        @strongify(self);
        NSLog(@"%@", self);
    }];

    return cell;
}

我们看到这里的RACObserve创建的Signal和self之间已经去掉了循环引用的问题,所以应该是没有什么问题的。但是结合之前我们对RACObserve的理解再仔细分析一下,这里的Signal只要self没有被dealloc的话就不会被释放。虽然每次UITableViewCell都会被重用,但是每次重用过程中创建的信号确实无法被disposable。那我们该怎么做呢?

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1000;
}

- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell"];

    @weakify(self);
    [[RACObserve(cell.textLabel, text) takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(id x) {
        @strongify(self);
        NSLog(@"%@", self);
    }];

    return cell;
}

注意,我们在cell里面创建的信号加上takeUntil:cell.rac_prepareForReuseSignal,这个是让cell在每次重用的时候都去disposable创建的信号。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 打算在项目中大面积使用RAC来开发,所以整理一些常用的实践范例和比较完整的api说明方便开发时随时查阅 声明式编程...
    星光社的戴铭阅读 5,408评论 5 49
  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 1,407评论 0 1
  • (转载自)http://limboy.me/tech/2013/12/27/reactivecocoa-2.htm...
    GeniusLi阅读 253评论 0 0
  • 简介 ReactiveCocoa(简称为RAC),RAC具有函数响应式编程特性,由Matt Diephouse开源...
    PHM阅读 13,064评论 4 53
  • (转载注明来自:http://hparis.github.io/blog/2015/07/25/rache-nei...
    Pariscode阅读 13,347评论 8 45