何千行も何も話していませんが、物事をこれほど高くスケールアップする方法があれば、私はそれが大好きです。
27セクションと180行がすべてのセクションに分散しているテーブルがあり、現在行き詰まっているシナリオは、3セクションと5行だけのモデル状態にアニメーション化し、(さらに悪いことに)再び元に戻す場合です。
beginUpdates/endUpdatesを使用してすべてのアニメーションをバッチ処理しています。私のアプリは、iphone4で1〜2秒間ロックされ、問題が解決されてから、アニメーションが開始されます。
私は、各行の削除/追加をアニメーション化すること、セクションを維持すること(および削除の場合は行数を0に落とすこと)、およびセクション自体の削除/挿入だけをアニメーション化すること(行数が0に低下しました)。後者の方がパフォーマンスが良いと思いましたが、まったく変わりませんでした。
これをスピードアップするためにアプリ側でできることはありますか?現在、アニメーションが20を超える場合は、個々のアニメーションを回避するためのかなり大まかなコードがあり、代わりにreloadDataを選択しています。
問題を示すコードをここで編集します。このコードのパフォーマンスは、同等のモノタッチコード(以前使用していたもの)よりもわずかに優れていますが、それでもかなり悪いです。
#import "TableViewController.h"
@interface MyTableViewDataSource : NSObject<UITableViewDataSource> {
int rows;
};
@end
@implementation MyTableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (void)setRowCount:(int)r
{
rows = r;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.text = [NSString stringWithFormat:@"row %d", indexPath.row];
return cell;
}
@end
@implementation MyTableViewController {
UIBarButtonItem *populateButtonItem;
};
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
populateButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Populate" style:UIBarButtonItemStylePlain target:self action:@selector(populateDataSource)];
}
return self;
}
- (void)populateDataSource
{
NSMutableArray* new_rows = [[NSMutableArray alloc] init];
[((MyTableViewDataSource*)self.tableView.dataSource) setRowCount:200];
for (int i = 0; i < 200; i ++)
[new_rows addObject:[NSIndexPath indexPathForRow:i inSection:0]];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:new_rows withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.dataSource = [[MyTableViewDataSource alloc] init];
self.navigationItem.rightBarButtonItem = populateButtonItem;
}
@end