ReactiveCocoaを使用する場合、UItableViewCellでUIbuttonがイベントをクリックするエラーの実装
5094 ワード
エラー実装方法
上記の方法では,複数のデータがある場合やページをリフレッシュした後,buttonをクリックすると何度もクリックイベントが削除される.データが多すぎたり、ページが更新されたりすると、UItableViewの多重化が有効になり、クリックイベントが複数回登録されます.
ReactiveCocoaの登録コードは以下の通りです.
ReactiveCocoaコードを参照すると、
解決策は
正しい実装方法
方法1クリックイベントを登録し、初期化cellに置く.
方法2 UITableViewCellでblockを定義し,UIButtonクリックイベントを実現し,blockを介してイベントを伝達する.
UItableViewセルの初期化時にblockを実現します.
メソッド3 rac_の使用commandはクリックイベントを実現する.
方法四(推奨)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellID = @"DSTestTableViewCell";
DSTestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[DSTestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
[[mcell.testBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
NSLog(@"click action");
}];
return cell;
}
上記の方法では,複数のデータがある場合やページをリフレッシュした後,buttonをクリックすると何度もクリックイベントが削除される.データが多すぎたり、ページが更新されたりすると、UItableViewの多重化が有効になり、クリックイベントが複数回登録されます.
ReactiveCocoaの登録コードは以下の通りです.
- (RACDisposable *)subscribeNext:(void (^)(id x))nextBlock {
NSCParameterAssert(nextBlock != NULL);
RACSubscriber *o = [RACSubscriber subscriberWithNext:nextBlock error:NULL completed:NULL];
return [self subscribe:o];
}
- (RACDisposable *)subscribe:(id)subscriber {
NSCParameterAssert(subscriber != nil);
RACCompoundDisposable *disposable = [RACCompoundDisposable compoundDisposable];
subscriber = [[RACPassthroughSubscriber alloc] initWithSubscriber:subscriber signal:self disposable:disposable];
NSMutableArray *subscribers = self.subscribers;
@synchronized (subscribers) {
[subscribers addObject:subscriber];
}
return [RACDisposable disposableWithBlock:^{
@synchronized (subscribers) {
// Since newer subscribers are generally shorter-lived, search
// starting from the end of the list.
NSUInteger index = [subscribers indexOfObjectWithOptions:NSEnumerationReverse passingTest:^ BOOL (id obj, NSUInteger index, BOOL *stop) {
return obj == subscriber;
}];
if (index != NSNotFound) [subscribers removeObjectAtIndex:index];
}
}];
}
ReactiveCocoaコードを参照すると、
RACSignal
のsubscribeNext
の実装は
であり、登録するたびに購読者信号量[subscribers addObject:subscriber];
が追加されるが、クリックイベントがトリガーされ、すべての購読者の信号量がトリガーされ、複数回のクリックイベントがトリガーされる.解決策は
です.正しい実装方法
方法1クリックイベントを登録し、初期化cellに置く.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellID = @"DSTestTableViewCell";
DSTestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[DSTestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
[[mcell.testBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
NSLog(@"click action");
}];
}
return cell;
}
方法2 UITableViewCellでblockを定義し,UIButtonクリックイベントを実現し,blockを介してイベントを伝達する.
[[_testBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
if (self.controlClickBlock) {
self.controlClickBlock
}
}];
UItableViewセルの初期化時にblockを実現します.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellID = @"DSTestTableViewCell";
DSTestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[DSTestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
mcell.controlClickBlock = ^() {
NSLog(@"click action");
};
return cell;
}
メソッド3 rac_の使用commandはクリックイベントを実現する.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellID = @"DSTestTableViewCell";
DSTestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[DSTestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
@weakify(self)
mcell.testBtn.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
@strongify(self)
NSLog(@"click action");
return [RACSignal empty];
}];
return cell;
}
方法四(推奨)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellID = @"DSTestTableViewCell";
DSTestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[DSTestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
[[[mcell.testBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:mcell.rac_prepareForReuseSignal] subscribeNext:^(id x) {
NSLog(@"click action");
}];
return cell;
}