我的应用程序的 UI 需要分页的 9 个图像的网格。为此,我在 UIScrollView(带有 UIPageControl)中创建了几个 UITable:
我的表来自我注册的 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 ?