搜尋

首頁  >  問答  >  主體

ios - 数据大造成的界面卡住问题



**我点击添加任务,然后再点击cell进入详情,发现需要卡顿一段时间才能进入详情,我添加任务做的操作是创建textView,让他成为第一相应,然后创建取消和添加俩个按钮,然后改变父视图的约束,以及隐藏添加任务的按钮,然后设置tableview的内边距以及设置让导航栏隐藏,
这个问题和https://segmentfault.com/q/10...里面的问题相似
问题在于数据少的时候不存在卡顿问题,但是数据多的话整个界面就会被卡死

望大神不吝赐教,由是感激**

具体代码如下

[addCard setTitle:@"添加任务" forState:UIControlStateNormal];
        [addCard setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [addCard setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        addCard.titleLabel.font = [UIFont systemFontOfSize:15];
        addCard.backgroundColor = [UIColor clearColor];
        self.addCards = addCard;
        // [addCard addTarget:self action:@selector(addCard:) forControlEvents:UIControlEventTouchUpInside];
        //使用ReactiveCocoa处理点击事件,相当于上面的addTarget:方法
        //addCard点击事件
        [[addCard rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            NSLog(@"%ld",(unsigned long)self.unNetArr.count);
            
            self.yesOrNo = NO;
            //隐藏addCard
            addCard.alpha = 0;
            //输入框
            UITextView *textView = ({
                UITextView *view = [UITextView new];
                [footer addSubview:view];
                [view makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(10);
                    make.top.equalTo(3);
                    make.right.equalTo(-10);
                    make.bottom.equalTo(-44+2);
                }];
                //因为footer变大,所以改变tableView的contentInset
                listTableView.contentInset = UIEdgeInsetsMake(46, 0, 88, 0);
                
                view.font = [UIFont systemFontOfSize:15];
                
                //立即刷新视图
                [self layoutIfNeeded];
                
                view;
            });
            self.textView = textView;
            
            //初始化add按钮
            UIButton *add = ({
                UIButton *view = [UIButton new];
                view.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
                [view setTitle:NSLocalizedString(@"add", nil) forState:UIControlStateNormal];
                [view setTitleColor:Global_trelloBlue forState:UIControlStateNormal];
                [view setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
                [view setTitleEdgeInsets:UIEdgeInsetsMake(0, -40, 0, -80)];
                [view.titleLabel setFont:[UIFont fontWithName:@"Arial-Bold" size:18]];
                view.titleLabel.font = [UIFont systemFontOfSize:15];
                [footer addSubview:view];
                [view makeConstraints:^(MASConstraintMaker *make) {
                    make.right.equalTo(0);
                    make.bottom.equalTo(-1);
                    make.width.equalTo(120);
                }];
                
                [view sizeToFit];
                //初始化后隐藏(为了动画效果)
                view.alpha = 0;
                //立即刷新
                [self layoutIfNeeded];
                
                //add点击事件,创建新的tast
                [[view rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
                    
                    //textView判空
                    if (![textView.text isEqualToString:@""]) {
                        self.yesOrNo = NO;
                        AddTaskApi *api = AddTaskApi.new;
                        //如果在没有task的列添加新的task,t_sort传0
                        api.t_sort = @(0);
                        //都上传0
                        api.t_taskContent = textView.text;
                        api.t_projectId = @(_project_id.integerValue);
                        //当前列的id
                        api.t_panel_id = @(listTableView.model.id.integerValue);
                        NSLog(@"<><>%@",_project_id);
                        //判断是否有网
                        [GLobalRealReachability reachabilityWithBlock:^(ReachabilityStatus status) {
                            switch (status)
                            {
                                case RealStatusNotReachable:
                                {
                                    NewTasklistModel *model = [[NewTasklistModel alloc]init];
                                    model.unloading = YES;
                                    model.t_taskContent = textView.text;
                                    textView.text = nil;
                                    model.t_sort = @"0";
                                    model.t_id = [_project_id integerValue];
                                    model.t_panel_id = listTableView.model.id;
                                    model.page = idx;
                                    
                                    [listTableView.model.task addObject:model];
                                    [self.unNetArr addObject:model];
                                    NSLog(@"%ld",(unsigned long)self.unNetArr.count);
                                    //插入行
                                    [listTableView insertRow:(listTableView.model.task.count-1)inSection:0 withRowAnimation:
                                     UITableViewRowAnimationBottom];
                                    
                                    //计算rowHeight
                                    CGFloat rowHeight = [self tableView:listTableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:listTableView.model.task.count-1 inSection:0]];
                                    //根据当前列高度和rowHeight决定列高度是否增加
                                    //updateConstraints:方法更新autolayout
                                    [listView updateConstraints:^(MASConstraintMaker *make) {
                                        //增加新的rowHeight后list超出允许最大高度,之前不满最大高度
                                        if (listTableView.cellTotalHeight > (kTotalCellMaxHeight+NavAndStatusBarHeight-_keyboardHeight-44) && (listTableView.cellTotalHeight-rowHeight) < (kTotalCellMaxHeight+NavAndStatusBarHeight-_keyboardHeight-44)) {
                                            make.height.equalTo(listView.yyHeight+((kTotalCellMaxHeight+NavAndStatusBarHeight-_keyboardHeight-44)-(listTableView.cellTotalHeight-rowHeight)));
                                        }
                                        //增加新的rowHeight后list没有超过允许最大高度
                                        if (listTableView.cellTotalHeight <= (kTotalCellMaxHeight+NavAndStatusBarHeight-_keyboardHeight-44)) {
                                            make.height.equalTo(listView.yyHeight+rowHeight);
                                        }
                                    }];
                                    //tableView滚动到最底部,显示新添加行
                                    [listTableView scrollToRow:listTableView.model.task.count-1 inSection:0 atScrollPosition:UITableViewScrollPositionBottom animated:NO];
                                    //autolayout动画
                                    [self layoutIfNeeded];
                                    //                                               [listTableView reloadRow:listTableView.model.task.count-1 inSection:0 withRowAnimation:UITableViewRowAnimationBottom];
                                    
                                    
                                    //改变原列的标题数量
                                    UITextField *sourceListTableHeaderTF=(UITextField *)[self viewWithTag:[listTableView.model.id integerValue]];
                                    sourceListTableHeaderTF.text=[NSString stringWithFormat:@"%@ %ld",listTableView.model.name,(unsigned long)listTableView.model.task.count];
                                    break;
                                }
                                case RealStatusUnknown:
                                {
                                    NewTasklistModel *model = [[NewTasklistModel alloc]init];
                                    model.unloading = YES;
                                    model.t_taskContent = textView.text;
                                    textView.text = nil;
                                    model.t_sort = @"0";
                                    model.t_id = [_project_id integerValue];
                                    model.t_panel_id = listTableView.model.id;
                                    model.page = idx;
                                    
                                    [listTableView.model.task addObject:model];
                                    [self.unNetArr addObject:model];
                                    
                                    //插入行
                                    [listTableView insertRow:(listTableView.model.task.count-1)inSection:0 withRowAnimation:
                                     UITableViewRowAnimationBottom];
                                    
                                    //计算rowHeight
                                    CGFloat rowHeight = [self tableView:listTableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:listTableView.model.task.count-1 inSection:0]];
                                    //根据当前列高度和rowHeight决定列高度是否增加
                                    //updateConstraints:方法更新autolayout
                                    [listView updateConstraints:^(MASConstraintMaker *make) {
                                        //增加新的rowHeight后list超出允许最大高度,之前不满最大高度
                                        if (listTableView.cellTotalHeight > (kTotalCellMaxHeight+NavAndStatusBarHeight-_keyboardHeight-44) && (listTableView.cellTotalHeight-rowHeight) < (kTotalCellMaxHeight+NavAndStatusBarHeight-_keyboardHeight-44)) {
                                            make.height.equalTo(listView.yyHeight+((kTotalCellMaxHeight+NavAndStatusBarHeight-_keyboardHeight-44)-(listTableView.cellTotalHeight-rowHeight)));
                                        }
                                        //增加新的rowHeight后list没有超过允许最大高度
                                        if (listTableView.cellTotalHeight <= (kTotalCellMaxHeight+NavAndStatusBarHeight-_keyboardHeight-44)) {
                                            make.height.equalTo(listView.yyHeight+rowHeight);
                                        }
                                    }];
                                    //tableView滚动到最底部,显示新添加行
                                    [listTableView scrollToRow:listTableView.model.task.count-1 inSection:0 atScrollPosition:UITableViewScrollPositionBottom animated:NO];
                                    //autolayout动画
                                    [self layoutIfNeeded];
                                    
                                    
                                    
                                    //改变原列的标题数量
                                    UITextField *sourceListTableHeaderTF=(UITextField *)[self viewWithTag:[listTableView.model.id integerValue]];
                                    sourceListTableHeaderTF.text=[NSString stringWithFormat:@"%@ %ld",listTableView.model.name,(unsigned long)listTableView.model.task.count];
                                    break;
                                }
                                    
                                    
                                default:
                                    //发起请求
                                    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
                                        SLog(@"<><>>>%@",request);
                                        NewTasklistModel *model = request.responseModel;
                                        model.unloading = NO;
                                        
                                        //存在即失败(接口定义,只在失败时才返回status值)
                                        if (model.status) {
                                            
                                        }
                                        else { //添加成功
                                            textView.text = nil;
                                            //更新数据源
                                            [listTableView.model.task addObject:model];
                                            //插入行
                                            [listTableView insertRow:(listTableView.model.task.count-1)inSection:0 withRowAnimation:
                                             UITableViewRowAnimationBottom];
                                            //计算rowHeight
                                            CGFloat rowHeight = [self tableView:listTableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:listTableView.model.task.count-1 inSection:0]];
                                            
                                            
                                            //根据当前列高度和rowHeight决定列高度是否增加
                                            //updateConstraints:方法更新autolayout
                                            
                                            [listView updateConstraints:^(MASConstraintMaker *make) {
                                                //增加新的rowHeight后list超出允许最大高度,之前不满最大高度
                                                if (listTableView.cellTotalHeight > (kTotalCellMaxHeight+NavAndStatusBarHeight-_keyboardHeight-44) && (listTableView.cellTotalHeight-rowHeight) < (kTotalCellMaxHeight+NavAndStatusBarHeight-_keyboardHeight-44)) {
                                                    make.height.equalTo(listView.yyHeight+((kTotalCellMaxHeight+NavAndStatusBarHeight-_keyboardHeight-44)-(listTableView.cellTotalHeight-rowHeight)));
                                                }
                                                
                                                //增加新的rowHeight后list没有超过允许最大高度
                                                if (listTableView.cellTotalHeight <= (kTotalCellMaxHeight+NavAndStatusBarHeight-_keyboardHeight-44)) {
                                                    make.height.equalTo(listView.yyHeight+rowHeight);
                                                }
                                            }];
                                            //tableView滚动到最底部,显示新添加行
                                            [listTableView scrollToRow:listTableView.model.task.count-1 inSection:0 atScrollPosition:UITableViewScrollPositionBottom animated:NO];
                                            [self layoutIfNeeded];
                                            //autolayout动画
                                            
                                            
                                            //改变原列的标题数量
                                            UITextField *sourceListTableHeaderTF=(UITextField *)[self viewWithTag:[listTableView.model.id integerValue]];
                                            sourceListTableHeaderTF.text=[NSString stringWithFormat:@"%@ %ld",listTableView.model.name,(unsigned long)listTableView.model.task.count];
                                            
                                        }
                                    } failure:^(YTKBaseRequest *request) {
                                        
                                    }];
                                    break;
                            }
                        }];
                        
                        
                        
                        
                        
                    }else{
                        self.yesOrNo = YES;
                        
                        [footer updateConstraints:^(MASConstraintMaker *make) {
                            make.height.equalTo(44);
                        }];
                        //list减去footer增加的高度(在这里做是为了动效)
                        [listView updateConstraints:^(MASConstraintMaker *make) {
                            make.height.equalTo(listView.yyHeight-44);
                        }];
                        //tableView的contentInset还原
                        listTableView.contentInset = UIEdgeInsetsMake(46, 0, 44, 0);
                        //隐藏textView
                        textView.alpha = 0;
                        
                        [UIView animateWithDuration:0.25 animations:^{
                            //cancel隐藏
                            view.alpha = 0;
                            
                            //add隐藏
                            self.cancel.alpha = 0;
                            //addCard显示
                            addCard.alpha = 1;
                            [self.viewController.navigationController setNavigationBarHidden:NO animated:YES];
                            [self layoutIfNeeded];
                        }];
                        [textView resignFirstResponder];
                        
                    }
                }];
                view;
            });
            self.addView = add;
            //初始化cancel按钮
            UIView *cancel = ({
                UIButton *view = [UIButton new];
                view.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
                [view setTitle:NSLocalizedString(@"cancel", nil) forState:UIControlStateNormal];
                [view setTitleColor:Global_trelloBlue forState:UIControlStateNormal];
                [view.titleLabel setFont:[UIFont fontWithName:@"Arial-Bold" size:18]];
                [view setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
                [view setTitleEdgeInsets:UIEdgeInsetsMake(0, -80, 0, -40)];
                view.titleLabel.font = [UIFont systemFontOfSize:15];
                [footer addSubview:view];
                [view makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(0);
                    make.bottom.equalTo(-1);
                    make.width.equalTo(120);
                }];
                [view sizeToFit];
                view.alpha = 0;
                [self layoutIfNeeded];
                //cancel点击事件
                @weakify(self, view, textView, footer, listView, add, addCard)
                [[view rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x)
                 {
                     @strongify(self, view, textView, footer, listView, add, addCard)
                     //footer高度还原
                     self.yesOrNo = YES;
                     [textView resignFirstResponder];
                     // list减去footer增加的高度(在这里做是为了动效)
                     [footer updateConstraints:^(MASConstraintMaker *make) {
                         make.height.equalTo(44);
                     }];
                     [listView updateConstraints:^(MASConstraintMaker *make) {
                         make.height.equalTo(listView.yyHeight);
                     }];
                     //  tableView的contentInset还原
                     listTableView.contentInset = UIEdgeInsetsMake(46, 0, 44, 0);
                     // 隐藏textView
                     textView.alpha = 0;
                     [UIView animateWithDuration:0.5 animations:^{
                         //cancel隐藏
                         view.alpha = 0;
                         //add隐藏
                         add.alpha = 0;
                         //addCard显示
                         addCard.alpha = 1;
                         [self.viewController.navigationController setNavigationBarHidden:NO animated:NO];
                         [self layoutIfNeeded];
                     }];
                 }];
                view;
天蓬老师天蓬老师2771 天前422

全部回覆(5)我來回復

  • 高洛峰

    高洛峰2017-04-18 09:56:53

    你貼這麼長的程式碼,怎麼看啊。
    這種問題的話,你應該看是哪裡佔據了比較長的時間。然後解決啊。
    個人覺得tableView的刷新次數可能比較多的話,會影響效能,你可以看下,是不是tableView的代理方法在一直被呼叫呢?

    回覆
    0
  • 天蓬老师

    天蓬老师2017-04-18 09:56:53

    回覆
    0
  • PHPz

    PHPz2017-04-18 09:56:53

    程式碼寫法逼格高的都看不懂了…

    回覆
    0
  • 迷茫

    迷茫2017-04-18 09:56:53

    問題終於解決了 謝謝你們

    回覆
    0
  • PHPz

    PHPz2017-04-18 09:56:53

    響應式程式設計?

    回覆
    0
  • 取消回覆