最近有稍微用過ReactiveCocoa,主要是用的Button谅辣,Button的target-action的模式,處理事件很不方便,特別是需要傳遞參數(shù)。發(fā)現(xiàn)ReactiveCocoa的block的方式比較好用校镐。先看看簡單的用法:
基本用法:
[[chatBtn rac_signalForControlEvents:UIControlEventTouchUpInside]
subscribeNext:^(id x) {
NSLog(@"x %@", x);
ProductionCellEntity *cellEntity = productList[indexPath.row];
[weakSelf goToChatView:cellEntity.entity.linkman];
}];
在UITableViewCell中使用Button,經(jīng)常我們需要判斷是那一個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 里面鸟廓,會被重復(fù)調(diào)用,如果不加上面那句話襟己,一個button就會有多個監(jiān)聽者引谜,button的事件代碼就會重復(fù)調(diào)用。