Arclin

Advocate Technology. Enjoy Technology.

0%

关于cell上的按钮点击后会重复发送信号的问题

RAC给UITableViewCell提供了一个方法:rac_prepareForReuseSignal,它的作用是当Cell即将要被重用时,告诉Cell。想象Cell上有多个button,Cell在初始化时给每个button都addTarget:action:forControlEvents,被重用时需要先移除这些target,下面这段代码就可以很方便地解决这个问题:

1
2
3
4
5
6
[[[self.cancelButton
rac_signalForControlEvents:UIControlEventTouchUpInside]
takeUntil:self.rac_prepareForReuseSignal]
subscribeNext:^(UIButton *x) {
// do other things
}];

[NSURL URLWithString:@”…………”] 返回空 nil
但是貌似汉字或者空格等无法被识别,String不被认为是URLString,这个NSURL的值也就一直是nil
要怎样才能够让它识别呢?
解决方法如下 :

  1. 转换编码
1
2
3
str1 = [str1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:[Tool returnFormatString:str1]];
  1. 除去空格
1
2
3
4
5
6
+(NSString *)returnFormatString:(NSString *)str
{
return [str stringByReplacingOccurrencesOfString:@" "withString:@" "];
}

NSLog(@"URL==%@",url);

ipad和iphone使用UIAlertViewController

1
2
3
4
5
6
7
8
9
10
11
id aController;
if(DKDeviceiPad){
alertC.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popPc = alertC.popoverPresentationController;
popPc.barButtonItem = self.downloadItem;
popPc.permittedArrowDirections = UIPopoverArrowDirectionAny;
aController = alertC;
}else{
aController = alertC;
}
[window.rootViewController presentViewController:aController animated:YES completion:nil];

pan手势判断方向

1
2
UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint point = [pan translationInView:gestureRecognizer.view]; // point.x < 0 左滑

什么时候用weakSelf 什么时候用 strongSelf

  • Objective C 的 Block 是一个很实用的语法,特别是与GCD结合使用,可以很方便地实现并发、异步任务。但是,如果使用不当,Block 也会引起一些循环引用问题(retain cycle)—— Block 会 retain ‘self’,而 ‘self‘ 又 retain 了 Block。因为在 ObjC 中,直接调用一个实例变量,会被编译器处理成 ‘self->theVar’,’self’ 是一个 strong 类型的变量,引用计数会加 1,于是,self retains queue, queue retains block,block retains self。

  • 解决 retain circle

    • Apple 官方的建议是,传进 Block 之前,把 ‘self’ 转换成 weak automatic 的变量,这样在 Block 中就不会出现对 self 的强引用。如果在 Block 执行完成之前,self 被释放了,weakSelf 也会变为 nil。
      示例代码:
1
2
3
4
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[weakSelf doSomething];
});
  • clang 的文档表示,在 doSomething 内,weakSelf 不会被释放。但,下面的情况除外:
1
2
3
4
5
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[weakSelf doSomething];
[weakSelf doOtherThing];
});
  • 在 doSomething 中,weakSelf 不会变成 nil,不过在 doSomething 执行完成,调用第二个方法 doOtherThing 的时候,weakSelf 有可能被释放,于是,strongSelf 就派上用场了:
1
2
3
4
5
6
__weak __typeof__(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
__strong typeof(self) strongSelf = weakSelf;
[strongSelf doSomething];
[strongSelf doOtherThing];
});
  • __strong 确保在 Block 内,strongSelf 不会被释放。

  • 总结

    • 在 Block 内如果需要访问 self 的方法、变量,建议使用 weakSelf。

    • 如果在 Block 内需要多次 访问 self,则需要使用 strongSelf。

发现一个好用的api,用于找出selectedItem 在 dataSource 里面的位置,适用于tableView和collectionView

1
2
3
4
5
6
NSIndexSet *indexSet = [self.photos indexesOfObjectsPassingTest:^BOOL(DKPhoto * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([self.selectedPhotoArray containsObject:obj]) {
return YES;
}
return NO;
}];