iOS 开发一些问题

1、GCD线程间的通信

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t mainQueue = dispatch_get_main_queue();
    dispatch_async(queue, ^{
   
        //这里是延时操作方法
        dispatch_async(mainQueue, ^{
            //刷新UI
        });
    });

2、iOS 11之后上拉刷新TableView cell乱跳问题
只需加下面三行代码即可。
estimatedRowHeight是一个预估高度,iOS11之前是为0,在iOS11下,这个值默认为44。

我们知道tableView是继承于ScrollView的,一个scrollView能滑动,需要设置contentSize,那么tableView的contentSize怎么来呢?iOS11之前,会调用tableView每一个cell的heightForRowAtIndexPath来算出整个高度,从而相加得出contentSize来,这一个步骤挺耗性能!

所以iOS11,默认打开了estimatedRowHeight估算高度功能,当tableView创建完成后,contentSize为estimatedRowHeight(默认值为44)*cell的数量,不需要遍历每一个cell的heightForRowAtIndexPath来计算了。

_tableView.estimatedRowHeight = 0;
_tableView.estimatedSectionHeaderHeight = 0;
_tableView.estimatedSectionFooterHeight = 0;

3、多个异步请求都结束后在刷新界面数据

dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_group_async(group, queue, ^{
        [self requestCycleData];
    });
    
    dispatch_group_async(group, queue, ^{
        [self heartTextRequest];
    });
    
    dispatch_group_async(group, queue, ^{
        [self recommendArticleImageRequest];
    });
    
    dispatch_group_async(group, queue, ^{
        [self articleListRequestWithPageNo:0 andPageSize:10];
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        //刷新数据
       
    });
}

requestCycleData

-(void)requestCycleData{
    dispatch_semaphore_t sem = dispatch_semaphore_create(0);
    [MMDHomeOrderRequest homeTopImageRequestWithBlock:^(MMDResponse *response) {
        dispatch_semaphore_signal(sem);
    }];
    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
}

4、Label字符串设置不同颜色
根据位置修改颜色

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"剩余%@分%@秒自动失效",str_minute,str_second]];
    [string addAttribute:NSForegroundColorAttributeName value:UIColor_Blue range:NSMakeRange(2,2)];
    [string addAttribute:NSForegroundColorAttributeName value:UIColor_Blue range:NSMakeRange(5,2)];
    self.timeLeftToPayLabel.attributedText = string;

根据字符串匹配修改颜色

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:format_time];
    NSRange range11 = [[str string] rangeOfString:[NSString stringWithFormat:@"%@分%@秒",str_minute,str_second]];
    [str addAttribute:NSForegroundColorAttributeName value:UIColor_Blue range:range11];
    NSRange range22 = [[str string] rangeOfString:@"剩余自动失效"];
    [str addAttribute:NSForegroundColorAttributeName value:UIColor_Text range:range22];
    self.timeLeftToPayLabel.attributedText  = str;

5、获取当前Window的controller视图

- (UIViewController *)topViewController {
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

- (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}

6、UIPageControl使用自定义图片间距设置
继承UIPageControl重写layoutSubviews和setCurrentPage方法

#import "MMDPageControl.h"

@implementation MMDPageControl

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

#define margin 8
#define dotNormalW 8
#define dotCurrentW 16




- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //计算整个pageControll的宽度
    CGFloat newW = (self.subviews.count - 1) * (dotNormalW + margin) + dotCurrentW;
    
    //设置新frame
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, newW, self.frame.size.height);
    
    //设置居中
    CGPoint center = self.center;
    center.x = self.superview.center.x;
    self.center = center;
    
    for (NSUInteger subviewIndex = 0; subviewIndex < [self.subviews count]; subviewIndex++) {
        UIImageView* subview = [self.subviews objectAtIndex:subviewIndex];
        CGSize size;
        
        if (subviewIndex == self.currentPage) {
            size.height = 2;
            size.width = dotCurrentW;
            [subview setFrame:CGRectMake(subviewIndex * (dotNormalW + margin), subview.frame.origin.y, size.width,size.height)];
        } else {
            size.height = 2;
            size.width = dotNormalW;
            if (subviewIndex > self.currentPage) {
                [subview setFrame:CGRectMake(dotCurrentW + (margin * subviewIndex) + (subviewIndex - 1) * dotNormalW, subview.frame.origin.y, size.width,size.height)];
            }else{
                [subview setFrame:CGRectMake(subviewIndex * (dotNormalW + margin), subview.frame.origin.y, size.width,size.height)];
            }
            
        }
    }
}

-(void)setCurrentPage:(NSInteger)currentPage{
    [super setCurrentPage:currentPage];
    for (NSUInteger subviewIndex = 0; subviewIndex < [self.subviews count]; subviewIndex++) {
        UIImageView* subview = [self.subviews objectAtIndex:subviewIndex];
        CGSize size;
        
        if (subviewIndex == currentPage) {
            size.height = 2;
            size.width = dotCurrentW;
            [subview setFrame:CGRectMake(subviewIndex * (dotNormalW + margin), subview.frame.origin.y, size.width,size.height)];
        } else {
            size.height = 2;
            size.width = dotNormalW;
            if (subviewIndex > currentPage) {
                [subview setFrame:CGRectMake(dotCurrentW + (margin * subviewIndex) + (subviewIndex - 1) * dotNormalW, subview.frame.origin.y, size.width,size.height)];
            }else{
                [subview setFrame:CGRectMake(subviewIndex * (dotNormalW + margin), subview.frame.origin.y, size.width,size.height)];
            }
            
        }
    }
}


@end

7、数组循环遍历 数据筛选 出错写法

 for (int i = 0; i < array.count; i++) {
                NSDictionary *dic = [NSDictionary dictionaryWithDictionary:array[i]];
                if ([dic hl_integerForkey:@"displayPosition"] == 0) {
                    [array removeObject:dic];
                }
            }

数组在遍历过程中只能修改、不能进行删除操作、切记这种错误的写法,不知道你这样写过没

你可能感兴趣的:(iOS 开发一些问题)