iOS開發小技巧:重新整理UITableView

來源:互聯網
上載者:User

標籤:

UITableView對於iOS開發人員來說一定不會陌生,很有可能你的APP很多介面都用到它。關於UITableView的文章,想必已經不計其數,沒事可以多看看。特別是UITableView最佳化的文章,非常值得仔細琢磨一番。

 

UITableView對於iOS開發人員來說一定不會陌生,很有可能你的APP很多介面都用到它。關於UITableView的文章,想必已經不計其數,沒事可以多看看。特別是UITableView最佳化的文章,非常值得仔細琢磨一番。

今天我們來看看如何重新整理UITableView的,一般情況下,重新整理UITableView,我們會直接調用reloadData方法。

重新整理UITableView

[self.tableView reloadData];

reloadData是重新整理整個UITableView,有時候,我們可能需要局部重新整理。比如:只重新整理一個cell、只重新整理一個section等等。這個時候在調用reloadData方法,雖然使用者看不出來,但是有些浪費資源。

重新整理局部cell

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];

這樣就可以很方便的重新整理第一個section的第一個cell。雖然看起來代碼多了,但是確實比較節省資源。盡量少的重新整理,也是UITableView的一種最佳化。

局部重新整理section

NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];

[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];

上面這段代碼是重新整理第0個section。

重新整理動畫

重新整理UITableView還有幾個動畫:

typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {

UITableViewRowAnimationFade,   //淡入淡出

UITableViewRowAnimationRight,  //從右滑入         // slide in from right (or out to right)

UITableViewRowAnimationLeft,   //從左滑入

UITableViewRowAnimationTop,     //從上滑入

UITableViewRowAnimationBottom,  //從下滑入

UITableViewRowAnimationNone,            // available in iOS 3.0

UITableViewRowAnimationMiddle,          // available in iOS 3.2.  attempts to keep cell centered in the space it will/did occupy

UITableViewRowAnimationAutomatic = 100  // available in iOS 5.0.  chooses an appropriate animation style for you

};

iOS開發小技巧:重新整理UITableView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.