私のアプリの UI には、ページ分割された 9 つの画像のグリッドが必要です。これを行うために、UIScrollView 内にいくつかの UITables を作成しています (UIPageControl を使用):
私のテーブルは、登録したxibファイルから来ています。各テーブル セルには 3 つのボタンがあります。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
int numPages = ([self.images count] + 8) / 9;
NSLog(@"Page count: %i", numPages);
// Set pages
self.pageControl.numberOfPages = numPages;
for (int i = 0; i < numPages; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [UIColor blackColor];
[self.scrollView addSubview:subview];
// Let's try and draw a table
UITableView *table = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
table.delegate = self;
table.dataSource = self;
table.tag = 100 + i;
table.separatorColor = [UIColor clearColor];
[table setBackgroundColor:[UIColor blackColor]];
[table registerNib:[UINib nibWithNibName:@"Cell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TestCell"];
[self.scrollView addSubview:table];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * numPages, self.scrollView.frame.size.height);
}
私の問題は、ビューの読み込みが遅い (5 秒以上) ことです。ボタンに読み込まれる画像はローカルであり、大きくはありません。
私の cellForRowAtIndexPath は次のようになります。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TestCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// What table/page are we looking at?
// Check the tag
int page = tableView.tag - 100;
NSLog(@"We are on page/table: %i", page);
// Configure the cell
// Our cells have 3 button images
// So we'll loop and set each image
for (int i = 0; i < 3; i++) {
// From our images array, the item we want
// Will be the row * 3 + i
int imageIndex = (page * 9) + (indexPath.row * 3) + i;
NSLog(@"Image index is: %i", imageIndex);
// Button tags are 10-12 (or 10 + i)
UIButton *button = (UIButton *)[cell viewWithTag:(10 + i)];
[button addTarget:self action:@selector(poseSelected:) forControlEvents:UIControlEventTouchUpInside];
// We have to make sure we don't go out of the bounds of
// our images array (might not have /3 exactly)
if (imageIndex > [self.images count] - 1) {
// We have an extra button, let's hide it
button.hidden = YES;
} else {
// Get the image and file nameefrom our array
NSDictionary *imageDict = (NSDictionary *)[self.images objectAtIndex:imageIndex];
NSString *fileName = [imageDict objectForKey:@"file"];
// Make the thumb name
NSString *thumbName = [fileName stringByReplacingOccurrencesOfString:@".jpg"
withString:@"-THUMB.jpg"];
// NSLog(@"Thumb name is: %@", thumbName);
// Set the button image
UIImage *btnImage = [UIImage imageNamed:thumbName];
[button setImage:btnImage forState:UIControlStateNormal];
}
}
return cell;
}
ログ出力を見ると、すべてのセルがすぐにセットアップされています。どうしてこれなの?
画面外のテーブル ビューをレンダリングしてはいけませんか? または、dequeueReusableCellWithIdentifier を間違った方法で使用していますか?