2015-08-03 2 views
1

Я создаю тестовое приложение для тестирования производительности этих трех методов при перезагрузке табличного представления.Производительность UITableView reloadData, reloadSections: withRowAnimation: и reloadRowsAtIndexPaths: withRowAnimation:

enter image description here

// 
// ViewController.m 
// TableViewSample 
// 
// Created by Antonio081014 on 8/2/15. 
// Copyright (c) 2015 Antonio081014.com. All rights reserved. 
// 

#import "ViewController.h" 

@interface ViewController() <UITableViewDataSource, UITableViewDelegate> 
@property (nonatomic, strong) NSArray *listOfCards; 
@property (nonatomic, strong) NSIndexPath *selectedIndexPath; 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    NSMutableArray *list = [NSMutableArray array]; 
    for (int i=0; i<15; i++) { 
     NSString *carName = [NSString stringWithFormat:@"Car%d", arc4random() % 15]; 
     [list addObject:carName]; 
    } 
    self.listOfCards = list; 
    self.tableView.delegate = self; 
    self.tableView.dataSource = self; 
    self.selectedIndexPath = nil; 

    UIBarButtonItem *table = [[UIBarButtonItem alloc] initWithTitle:@"Table" style:UIBarButtonItemStylePlain target:self action:@selector(reloadTable:)]; 
    UIBarButtonItem *section = [[UIBarButtonItem alloc] initWithTitle:@"Section" style:UIBarButtonItemStylePlain target:self action:@selector(reloadSection:)]; 
    UIBarButtonItem *indexPath = [[UIBarButtonItem alloc] initWithTitle:@"IndexPath" style:UIBarButtonItemStylePlain target:self action:@selector(reloadRow:)]; 

    self.navigationItem.rightBarButtonItems = @[table, section, indexPath]; 
// self.navigationController.navigationItem.rightBarButtonItems = @[table, section, indexPath]; 
} 

- (void)reloadTable:(UIBarButtonItem *)barItem 
{ 
    [self.tableView reloadData]; 
} 

- (void)reloadRow:(UIBarButtonItem *)barItem 
{ 
    [self reloadRowAtIndexPath:self.selectedIndexPath forBarButtonItem:barItem]; 
} 

- (void)reloadSection:(UIBarButtonItem *)barItem 
{ 
    [self reloadSectionAt:0 forBarButtonItem:barItem]; 
} 

- (void)reloadRowAtIndexPath:(NSIndexPath *)indexPath forBarButtonItem:(UIBarButtonItem *)barItem 
{ 
    if (indexPath) { 
     [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    } 
} 

- (void)reloadSectionAt:(NSUInteger)section forBarButtonItem:(UIBarButtonItem *)barItem 
{ 
    [self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    NSLog(@"Asking Number of Sections in TableView"); 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSLog(@"Asking Number of Rows in Section"); 
    return self.listOfCards.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

    NSString *carName = self.listOfCards[indexPath.row]; 
    cell.textLabel.text = carName; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    self.selectedIndexPath = indexPath; 
    NSLog(@"Did Select Cell %@", indexPath); 
} 

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    self.selectedIndexPath = nil; 
    NSLog(@"Did Deselect Cell %@", indexPath); 
} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"Will Display Cell %@", indexPath); 
} 

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"Did End Display Cell %@", indexPath); 
} 

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"Did Highlight Cell %@", indexPath); 
} 

@end 

Log, когда три метода называется.

 
ReloadData 
2015-08-03 11:00:51.556 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 11} 
2015-08-03 11:00:51.558 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 10} 
2015-08-03 11:00:51.559 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 9} 
2015-08-03 11:00:51.560 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 8} 
2015-08-03 11:00:51.560 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 7} 
2015-08-03 11:00:51.560 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 6} 
2015-08-03 11:00:51.561 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 5} 
2015-08-03 11:00:51.561 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 4} 
2015-08-03 11:00:51.562 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 3} 
2015-08-03 11:00:51.563 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 2} 
2015-08-03 11:00:51.563 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 1} 
2015-08-03 11:00:51.564 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 0} 
2015-08-03 11:00:51.564 TableViewSample[324:90811] Asking Number of Sections in TableView 
2015-08-03 11:00:51.565 TableViewSample[324:90811] Asking Number of Rows in Section 
2015-08-03 11:00:51.566 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 0} 
2015-08-03 11:00:51.567 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 1} 
2015-08-03 11:00:51.567 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 2} 
2015-08-03 11:00:51.568 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 3} 
2015-08-03 11:00:51.569 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 4} 
2015-08-03 11:00:51.569 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 5} 
2015-08-03 11:00:51.570 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 6} 
2015-08-03 11:00:51.571 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 7} 
2015-08-03 11:00:51.572 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 8} 
2015-08-03 11:00:51.573 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 9} 
2015-08-03 11:00:51.573 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 10} 
2015-08-03 11:00:51.574 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 11} 


ReloadSection 
2015-08-03 11:02:21.641 TableViewSample[324:90811] Asking Number of Sections in TableView 
2015-08-03 11:02:21.642 TableViewSample[324:90811] Asking Number of Sections in TableView 
2015-08-03 11:02:21.643 TableViewSample[324:90811] Asking Number of Rows in Section 
2015-08-03 11:02:21.647 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 0} 
2015-08-03 11:02:21.649 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 1} 
2015-08-03 11:02:21.651 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 2} 
2015-08-03 11:02:21.653 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 3} 
2015-08-03 11:02:21.655 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 4} 
2015-08-03 11:02:21.657 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 5} 
2015-08-03 11:02:21.659 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 6} 
2015-08-03 11:02:21.662 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 7} 
2015-08-03 11:02:21.664 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 8} 
2015-08-03 11:02:21.666 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 9} 
2015-08-03 11:02:21.669 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 10} 
2015-08-03 11:02:21.671 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 11} 
2015-08-03 11:02:21.990 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 0} 
2015-08-03 11:02:21.991 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 1} 
2015-08-03 11:02:21.992 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 2} 
2015-08-03 11:02:21.992 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 3} 
2015-08-03 11:02:21.993 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 4} 
2015-08-03 11:02:21.994 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 5} 
2015-08-03 11:02:21.994 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 6} 
2015-08-03 11:02:21.995 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 7} 
2015-08-03 11:02:21.995 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 8} 
2015-08-03 11:02:21.996 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 9} 
2015-08-03 11:02:21.997 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 10} 
2015-08-03 11:02:21.997 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 11} 

ReloadRow 
2015-08-03 11:03:00.012 TableViewSample[324:90811] Did Highlight Cell {length = 2, path = 0 - 3} 
2015-08-03 11:03:00.015 TableViewSample[324:90811] Did Select Cell {length = 2, path = 0 - 3} 
2015-08-03 11:03:00.907 TableViewSample[324:90811] Asking Number of Sections in TableView 
2015-08-03 11:03:00.908 TableViewSample[324:90811] Asking Number of Sections in TableView 
2015-08-03 11:03:00.909 TableViewSample[324:90811] Asking Number of Rows in Section 
2015-08-03 11:03:00.910 TableViewSample[324:90811] Will Display Cell {length = 2, path = 0 - 3} 
2015-08-03 11:03:01.217 TableViewSample[324:90811] Did End Display Cell {length = 2, path = 0 - 3} 

Так, из журнала: [UITableView reloadData] потребляет 16мс. [UITableView reloadSections: withRowAnimation:] потребляет 323 мс. [UITableView reloadRowsAtIndexPaths: withRowAnimation:] потребляет 302 мс.

Вопрос:

  • Почему [UITableView reloadData] является более эффективным, чем reloadingSections даже есть только один раздел?
  • Зачем перезагружатьRowsAtIndexPaths занимает столько времени, что на самом деле занимает время?
  • Какие инструменты могут помочь мне проверить или отладить подобные проблемы? и справочную ссылку ссылки, если таковая имеется?
  • Спасибо.
+0

'reloadData' не обязано (необязательно) обновлять отдельные ячейки. –

+0

(И я замечаю, что вы на самом деле не показываете нам никакого кода.) –

+0

@HotLicks, Извините за код, я попытался отформатировать его правильно, но не повезло. Можете ли вы проверить исходный код при попытке «Изменить»? Огромное спасибо. – antonio081014

ответ

0

here, яблоко сказал:

перегрузочный строку вызывает вид таблицы, чтобы задать источник данных для новой ячейки для этой строки. Таблица оживляет новую ячейку, поскольку она оживляет старую строку.

, кажется, что, когда вы звоните reloadrowsatindexpaths, новая ячейка будет инициализирован для анимации, в то время как reloadData, как мы уже знали, будет, прежде чем повторно использовать ячейку.

Я думаю, что это может объяснить вопрос.