记录开发过程中的一些小问题汇总

1.计算NSAttributedString字符串的高度问题(字符串中带有高亮的富文本)

 CGSize textBlockMinSize = {(KScreenWidth -115), CGFLOAT_MAX};
    NSAttributedString *text = model.attributedContent;
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:0.01];//设置行高
    CGSize size = [[text string] boundingRectWithSize:textBlockMinSize
                              options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                           attributes:@{
                                        NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:14.0f],
                                        NSParagraphStyleAttributeName:paragraphStyle
                                        }
                              context:nil].size;

2.后台获取分页数据,循环请求

__block NSMutableArray *temp = [NSMutableArray new];
    // 1.创建一个串行队列,保证for循环依次执行
    dispatch_queue_t serialQueue1 = dispatch_queue_create("serialQueue1", DISPATCH_QUEUE_SERIAL);
    
    // 2.异步执行任务
    dispatch_async(serialQueue1, ^{
        // 3.创建一个数目为1的信号量,用于“卡”for循环,等上次循环结束在执行下一次的for循环
        dispatch_semaphore_t sema1 = dispatch_semaphore_create(1);
        static NSInteger page = 1;
        for (NSInteger j = 0; j<50; j++) {
            
            dispatch_semaphore_wait(sema1, DISPATCH_TIME_FOREVER);
            printf("信号量等待中\n");
            if (j != 0 && temp.count ==0) {
                dispatch_semaphore_signal(sema1);
                break;
            }
            //异步任务
            NSMutableDictionary *params  = [NSMutableDictionary  new];
            [params setObject:[NSNumber numberWithInteger:page] forKey:@"pageNum"];
            [params setObject:[NSNumber numberWithInteger:10] forKey:@"pageSize"];
            
            [[Server getInstance] baserequest:nil :GROUPMEMBERS :params :^(id requsetStr) {
                if (requsetStr !=0 && requsetStr !=nil) {
                    
                    if ([requsetStr count] == 0) {
                        page =1;
                        temp = [NSMutableArray new];
                        dispatch_semaphore_signal(sema1);
                        
                    }
                    else{
                        page++;
                        temp = [[NSMutableArray alloc]initWithArray:requsetStr];
                        dispatch_semaphore_signal(sema1);
                    }
                    //这里处理获取到的数据
                    for (NSDictionary *dic in (NSArray *)requsetStr) {
                        MemberModel *model = [[MemberModel alloc]initWithDictionary:dic];
                        if (model.role == 2 ||model.role ==1) {
                            [self.adminsArr addObject:model];
                        }
                        else{
                            [self.usersArr addObject:model];
                        }
                    }
                  //数组排序
                    NSSortDescriptor *role = [NSSortDescriptor sortDescriptorWithKey:@"role" ascending:YES];
                    NSSortDescriptor *gmtModified = [NSSortDescriptor sortDescriptorWithKey:@"gmtModified" ascending:YES];
                    self.adminsArr = [[self.adminsArr sortedArrayUsingDescriptors:@[role,gmtModified]] mutableCopy];
                    
                    [self.listView reloadData];
                    
                }
                else{
                    
                }
            }];
        }
        NSLog(@"其他操作");
        
    });
    

你可能感兴趣的:(记录开发过程中的一些小问题汇总)