UITableView停止載入中的動畫的順序問題

weixin_34321977發表於2017-01-10

在列表中我們經常使用下拉重新整理和上提更新的功能,在網路請求資料返回之後,我們會停止重新整理,但是何時停止重新整理呢?在資料返回之後立刻停止重新整理嗎?還是在儲存資料的變數賦值完成之後呢?
在我們的專案中,有一個獲取優惠券資料的介面,程式碼如下

[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];
         }
     }
 }]
 
 
 
程式設計是一種修行,時刻要謹慎小心!

相關文章