博文

目前显示的是 十月, 2015的博文

dispatch_group_enter和dispatch_group_leave的使用

dispatch_group_enter和dispatch_group_leave的使用,这两个方法用于把一个异步任务加入group里。 首先,这两个一定要成对使用,不然会crash。 一般我们要把一个任务加入一个group里是这样: dispatch_group_async(group, queue, ^{ block(); }); 这个写法等价于 dispatch_async(queue, ^{ dispatch_group_enter(group); block() dispatch_group_leave(group); }); 如果要把一个异步任务加入group,这样就行不通了: dispatch_group_async(group, queue, ^{ [self performBlock:^(){ block(); }]; //未执行到block() group任务就已经完成了 }); 这时需要这样写: dispatch_group_enter(group); [self performBlock:^(){ block(); dispatch_group_leave(group); }]; 异步任务回调后才算这个group任务完成。 其实这跟retain/release差不多,都是计数,dispatch_group_enter时任务数+1,dispatch_group_leave时任务数-1,任务数为0时执行dispatch_group_notify的内容。

TableView实现Switch的方法

直接修改accessoryView即可。 CellForRow方法里写: cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"]; UISwitch *switch=[[UISwitch alloc]init];  [switch addTarget:self action:@selector(switchValueChange:) forControlEvents:UIControlEventValueChanged];  cell.accessoryView=switch;

TableView横用导致的屏幕旋转的页码错乱问题。

今天偶然遇到了这个问题,因为这个tableview是横用作Scrollview的图片轮播器使用的,但是当屏幕旋转的时候会出现显示错乱问题(就是说旋转屏幕后会出现显示出别的cell)。 期初是想在屏幕旋转后,根据currentOffset计算以后的offset,但是可能是由于有navigation bar的问题,所以导致计算出问题,用windows的frame来计算也会出现错误,故抛弃。 经过研究后发现了暂时的解决方案: 先定义一个IndexPath*类型的全局变量然后先把当前的IndexPath存起来,在cellForRow里面对这个IndexPath进行赋值(因为一屏幕只显示一个cell,所以IndexPath一定是当前屏幕的IndexPath)。然后在屏幕开始旋转前,禁止对IndexPath赋值,然后旋转后,将IndexPath取出,然后用 [_tableview scrollToRowAtIndexPath:_index atScrollPosition:UITableViewScrollPositionMiddle animated:NO]; 然后 在开始旋转的时候把tableview的alpha=0;旋转后再=1就可以啦~

忘记设置userInteractionEnabled导致的各种弱智问题。

比如 什么 UIButton 里加了view 忘了设置为NO 比如 什么。。。各种下层需要触摸时间然后被上层view截获了的问题。。 一定要想着关掉 = =b

偶尔写写

其实也没啥好些的,意思意思吧。。 嗯,就是这个意思。

UICollectionView的重用问题

1、在Cell类型不同的时候,需要事先注册不同的identifier才能在 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 中使用。 2、Cell类型相同的时候,在Cell里一定要调用 prepareForReuse方法来保持重用的cell的清洁。 具体方法就是把需要赋值的对象=nil即可。但是注意在方法内调用[super prepareForReuse]; 还有一点。很急,很关键。 偶然发现了prepareForReuse方法只要cell滑出Collection的显示范围就会执行这个方法,那就为了解决重用问题,可以小小的trick一下。 在注册cell类的时候, for (int i = 0; i<_dataArray.count; i++) { [_mainCollectionView registerClass:[MainViewCollectionViewCell class]forCellWithReuseIdentifier:[NSString stringWithFormat:@"main%d",i]]; } 在给cell赋值的方法中,使用: NSString *identifier = [NSString stringWithFormat:[NSString stringWithFormat:@"main%d",indexPath.row]]; MainViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 轻松&愉快的完成了“准”重用过程~

UIScrollView在pagingEnabled=YES,然后旋转屏幕后的错误显示问题

屏幕旋转后会停在页面中间的位置。 今天偶然遇到了这个问题, 首先用了之前解决自定义Transition的屏幕旋转bounds的设置问题的解决方法,开线程! 等待系统屏幕旋转后再进行bounds的设置,旋转前设置相当于没设置,所以开一个零点几秒的延迟来做这个。 在以下这两个方法中开线程: 1:ViewController的方法: -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration; 2:监视系统通知: [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil]; - (void)orientationChanged:(NSNotification *)notification; 方法中添加的线程代码: dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{    UIWindow *window = [UIApplication sharedApplication].keyWindow; CGFloat x = _pageControl.current...