ReactiveCocoa Button在TableViewCell的应用

最近有稍微用过ReactiveCocoa,主要是用的Button,Button的target-action的模式,处理事件很不方便,特别是需要传递参数。发现ReactiveCocoa的block的方式比较好用。先看看简单的用法:

基本用法:

  [[chatBtn rac_signalForControlEvents:UIControlEventTouchUpInside]
     subscribeNext:^(id x) {
        NSLog(@"x %@", x);
        ProductionCellEntity *cellEntity = productList[indexPath.row];
        [weakSelf goToChatView:cellEntity.entity.linkman];
    }];

在UITableViewCell中使用Button,经常我们需要判断是那一个Cell的Btn点击的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseName];
    if (cell == nil) {
        cell = [[ProductTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseName];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }
    
    WS(weakSelf)
    cell.entity = productList[indexPath.row];
    [[[cell.chatBtn rac_signalForControlEvents:UIControlEventTouchUpInside]
      takeUntil:cell.rac_prepareForReuseSignal]
     subscribeNext:^(id x) {
        ProductionCellEntity *cellEntity = productList[indexPath.row];
        [weakSelf goToChatView:cellEntity.entity.linkman];
    }];

    return cell;
}

记住这个:

  takeUntil:cell.rac_prepareForReuseSignal

在cellForRowAtIndexPath 里面,会被重复调用,如果不加上面那句话,一个button就会有多个监听者,button的事件代码就会重复调用。

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

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,386评论 30 472
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,261评论 4 61
  • 准确性练习9--经济专题 1.The index of leading economic indicators f...
    王大可_辛酉阅读 708评论 0 0
  • 周日晚上送珏回学校的路上,总想和珏说点什么,但一周六天半长时间的学习,如果我还和他讨论学习势必引起厌烦情绪,该聊点...
    飞驰的风阅读 1,352评论 0 1
  • 为了实现webview向上滑动可以隐藏title栏,目前最简便的方法就是使用CordinatorLayout,要在...
    E木聿一阅读 7,713评论 1 4