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];
}
}
}]
程式設計是一種修行,時刻要謹慎小心!
相關文章
- sql中的or與and的執行順序問題SQL
- MapStruct與lombok載入順序問題與annotationProcessorPaths的關係?StructLombok
- Python 模組的載入順序Python
- javascript載入順序JavaScript
- iOS UITableView Cell和 SectionHeader 的呼叫順序iOSUIViewHeader
- js——<script>標籤的載入順序JS
- SQL中rownum和order by的執行順序的問題SQL
- Git diff hash順序的問題Git
- java類載入順序Java
- [Web]HTML載入順序WebHTML
- Spring如何控制Bean的載入順序SpringBean
- Jetpack Compose的Modifier順序問題Jetpack
- 如何克服 Apache Kafka中的資料順序問題 - DATAVERSITYApacheKafka
- Java父子類載入順序Java
- 頁面載入順序jQueryjQuery
- windows驅動載入順序Windows
- 影像延遲載入 && 列表圖順序載入
- web.xml 中的listener、 filter、servlet 載入順序及其詳解WebXMLFilterServlet
- 簡單的演算法-解決頁面指令碼非同步載入順序問題演算法指令碼非同步
- Script載入順序 & 外部樣式表的阻塞
- 關於jvm載入類的實現順序JVM
- SpringBoot配置檔案優先順序載入順序Spring Boot
- 無順序約束的字串匹配問題字串匹配
- 使用者登入時的環境載入順序
- Tomcat的class載入的優先順序一覽Tomcat
- 演算法題———————輸入棧的入棧順序和出棧順序判斷是否合理演算法
- 如何正確控制springboot中bean的載入順序總結Spring BootBean
- java中帶繼承類的載入順序詳解及實戰Java繼承
- ppt動畫出現順序怎麼設定 PPT設定動畫文字順序動畫
- 詳解web.xml中元素的載入順序WebXML
- Javascript在頁面載入時的執行順序JavaScript
- MySQL:讀取my.cnf的順序問題MySql
- SQL語句中的AND和OR執行順序問題SQL
- MYSQL INNODB中表資料的返回順序問題MySql
- OC 中非同步順序載入用法非同步
- mysql order by 和 group by 順序問題MySql
- Java 修飾符順序問題Java 修飾符
- iOS,UIViewController和nib方法的載入順序iOSUIViewController