UITableView停止加载中的动画的顺序问题

在列表中我们经常使用下拉刷新和上提更新的功能,在网络请求数据返回之后,我们会停止刷新,但是何时停止刷新呢?在数据返回之后立刻停止刷新吗?还是在保存数据的变量赋值完成之后呢?
在我们的项目中,有一个获取优惠券数据的接口,代码如下

[Coupon myCouponListSpecificWithType:couponType andGetNum:1 pageType:self.pageTypeOption block:^(Coupon *coupon, NSInteger type, NSString *notUsedNum, NSString *usedNum, NSString *overdueNum, NSString *error, NSInteger errorCode) {
        
        if (weakSelf) {
            typeof(self) strongSelf = weakSelf;
            if (strongSelf->couponType != type) {
                return ;
            }
            
            UITableView *tableView = nil;
            if (type == 0) {
                tableView = strongSelf->usableTableView;
            } else if (type == 1) {
                tableView = strongSelf->unusableTableView;
            } else {
                tableView = strongSelf->overdueTableView;
            }
            if (tableView.pullToRefreshView.state == SVPullToRefreshStateLoading) {
                [tableView.pullToRefreshView stopAnimating];
            }
            if (tableView.infiniteScrollingView.state == SVInfiniteScrollingStateLoading) {
                [tableView.infiniteScrollingView stopAnimating];
            }
            strongSelf.isExchangeDoneFlag = NO;
            
            if (error) {
                if (errorCode != 0) {
                    [strongSelf showBadNetworkView];
                } else {
                    [self hiddenViewWhenNoNetWork:NO];
                    [[Util defaultUtil] showAlert:error];
                }
            } else {
                [self hiddenViewWhenNoNetWork:NO];

                [strongSelf.badNetWorkView disappear];
                
                [strongSelf removeAllBadNetwrokView];
                strongSelf->isDownloadFinishData = YES;
                strongSelf->couponModel = coupon;
                
                strongSelf->notUsedCouponNumber = [notUsedNum integerValue];
                strongSelf->usedCouponNumber = [usedNum integerValue];
                strongSelf->overdueCouponNumber = [overdueNum integerValue];
                [strongSelf updateBtnTitleWithNumber:notUsedNum andUseNum:usedNum andOverdueNum:overdueNum]; //更新页签标题
                
                if (coupon.reduceCouponsArray.count==0 && coupon.cashCouponsArray.count==0) {
                    
                    CGRect rect = strongSelf.noCouponImageView.frame;
                    rect.origin.x = strongSelf->couponType * strongSelf.view.frame.size.width + (strongSelf.view.frame.size.width - 79)/2;
                    strongSelf.noCouponImageView.frame = rect;
                    
                    rect = strongSelf.noCouponLabel.frame;
                    rect.origin.x = couponType * strongSelf.view.frame.size.width + (strongSelf.view.frame.size.width - 200)/2;
                    strongSelf.noCouponLabel.frame = rect;
                    
                    [strongSelf->listView addSubview:strongSelf.noCouponImageView];
                    [strongSelf->listView addSubview:strongSelf.noCouponLabel];
                    [tableView setHidden:YES];
                }else {
                    if (strongSelf.noCouponImageView) {
                        [strongSelf.noCouponImageView removeFromSuperview];
                        [strongSelf.noCouponLabel removeFromSuperview];
                    }
                    if (strongSelf->couponID) {
                        strongSelf->exchangeCouponIndexPath = [strongSelf obtainExchangeCouponIndexPath];
                        DLog(@"indexPath:%@,couponID:%@", strongSelf->exchangeCouponIndexPath, strongSelf->couponID);
                    }

                    [tableView setHidden:NO];
                }
            }
            //0-未使用 1-已使用 2-已过期
            DLog(@"couponType:%ld", (long)type);
            if (type == 0) {
                [strongSelf->usableTableView reloadData];
                //兑换成功后刷新,跳转至可用列表的相应位置
                if (strongSelf->couponID) {
                    [strongSelf->usableTableView scrollToRowAtIndexPath:exchangeCouponIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
                    strongSelf->couponID = nil;
                }
            } else if (type == 1) {
                [strongSelf->unusableTableView reloadData];
            } else if (type == 2) {
                [strongSelf->overdueTableView reloadData];
            }
        }
    }];
   
   
   初看代码,数据返回来之后判断数据返回的类型是否跟请求的类型一致,如果不一致则不进行任何处理,然后停止加载中的动画,数据处理,表格刷新!
   问题恰恰出现在先停止加载中的动画,这个时候系统会调用
   

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
这个方法,那么数据源我还没有改变,还是使用之前的数据源,那么就会崩溃喽
所以正确的做法是在更新完数据源之后,在进行停止加载中的动画,代码如下:

[Coupon myCouponListSpecificWithType:couponType andGetNum:1 pageType:self.pageTypeOption block:^(Coupon *coupon, NSInteger type, NSString *notUsedNum, NSString *usedNum, NSString *overdueNum, NSString *error, NSInteger errorCode) {
     
     if (weakSelf) {
         typeof(self) strongSelf = weakSelf;
         if (strongSelf->couponType != type) {
             return ;
         }
         
         UITableView *tableView = nil;
         if (type == 0) {
             tableView = strongSelf->usableTableView;
         } else if (type == 1) {
             tableView = strongSelf->unusableTableView;
         } else {
             tableView = strongSelf->overdueTableView;
         }
         
         strongSelf.isExchangeDoneFlag = NO;
         
         if (error) {
             if (errorCode != 0) {
                 [strongSelf showBadNetworkView];
             } else {
                 [self hiddenViewWhenNoNetWork:NO];
                 [[Util defaultUtil] showAlert:error];
             }
         } else {
             [self hiddenViewWhenNoNetWork:NO];

             [strongSelf.badNetWorkView disappear];
             
             [strongSelf removeAllBadNetwrokView];
             strongSelf->isDownloadFinishData = YES;
             strongSelf->couponModel = coupon;
             
             strongSelf->notUsedCouponNumber = [notUsedNum integerValue];
             strongSelf->usedCouponNumber = [usedNum integerValue];
             strongSelf->overdueCouponNumber = [overdueNum integerValue];
             [strongSelf updateBtnTitleWithNumber:notUsedNum andUseNum:usedNum andOverdueNum:overdueNum]; //更新页签标题
             
             if (coupon.reduceCouponsArray.count==0 && coupon.cashCouponsArray.count==0) {
                 
                 CGRect rect = strongSelf.noCouponImageView.frame;
                 rect.origin.x = strongSelf->couponType * strongSelf.view.frame.size.width + (strongSelf.view.frame.size.width - 79)/2;
                 strongSelf.noCouponImageView.frame = rect;
                 
                 rect = strongSelf.noCouponLabel.frame;
                 rect.origin.x = couponType * strongSelf.view.frame.size.width + (strongSelf.view.frame.size.width - 200)/2;
                 strongSelf.noCouponLabel.frame = rect;
                 
                 [strongSelf->listView addSubview:strongSelf.noCouponImageView];
                 [strongSelf->listView addSubview:strongSelf.noCouponLabel];
                 [tableView setHidden:YES];
             }else {
                 if (strongSelf.noCouponImageView) {
                     [strongSelf.noCouponImageView removeFromSuperview];
                     [strongSelf.noCouponLabel removeFromSuperview];
                 }
                 if (strongSelf->couponID) {
                     strongSelf->exchangeCouponIndexPath = [strongSelf obtainExchangeCouponIndexPath];
                     DLog(@"indexPath:%@,couponID:%@", strongSelf->exchangeCouponIndexPath, strongSelf->couponID);
                 }

                 [tableView setHidden:NO];
             }
         }
         if (tableView.pullToRefreshView.state == SVPullToRefreshStateLoading) {
             [tableView.pullToRefreshView stopAnimating];
         }
         if (tableView.infiniteScrollingView.state == SVInfiniteScrollingStateLoading) {
             [tableView.infiniteScrollingView stopAnimating];
         }
         //0-未使用 1-已使用 2-已过期
         DLog(@"couponType:%ld", (long)type);
         if (type == 0) {
             [strongSelf->usableTableView reloadData];
             //兑换成功后刷新,跳转至可用列表的相应位置
             if (strongSelf->couponID) {
                 [strongSelf->usableTableView scrollToRowAtIndexPath:exchangeCouponIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
                 strongSelf->couponID = nil;
             }
         } else if (type == 1) {
             [strongSelf->unusableTableView reloadData];
         } else if (type == 2) {
             [strongSelf->overdueTableView reloadData];
         }
     }
 }]
 
 
 
编程是一种修行,时刻要谨慎小心!

你可能感兴趣的:(UITableView停止加载中的动画的顺序问题)