您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

UITableView 的局部刷新技巧

編輯:iOS 開發綜合

在更新表視圖時,並非每次都需要呼叫 reloadData 重繪整個列表。針對只變動部分內容的情況,可以採用局部刷新,不僅更有效率,還能搭配動畫呈現更流暢的視覺效果。

局部刷新某個 section

使用 reloadSections:withRowAnimation:,透過 NSIndexSet 指定要刷新的節:

NSIndexSet *nd = [[NSIndexSet alloc] initWithIndex:1]; // 刷新第二個 section
[tview reloadSections:nd withRowAnimation:UITableViewRowAnimationAutomatic];

局部刷新某個 cell

使用 reloadRowsAtIndexPaths:withRowAnimation:,透過 NSIndexPath 指定要刷新的列:

NSIndexPath *te = [NSIndexPath indexPathForRow:2 inSection:0]; // 刷新第一個 section 的第二行
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:te, nil] withRowAnimation:UITableViewRowAnimationMiddle];

其中 withRowAnimation: 參數可傳入多種預設動畫,如 UITableViewRowAnimationAutomatic、UITableViewRowAnimationMiddle 等,讓刷新過程帶有淡入淡出或滑動的效果。相較於整表重載,局部刷新只更新受影響的區塊,既省資源又能保留其他列的狀態,是優化表視圖體驗的實用技巧。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved