我正在以编程方式创建许多表格视图。客户特别要求一个不是 UITableView 控制器的布局。就像是:
表格视图的数量以及每个表格中的项目数量各不相同。加载视图时会立即创建所有表。我想为表格视图中的单元格添加缩略图。我需要找到一种延迟加载缩略图的方法。我已经成功地在 UITableViewController 上实现了延迟加载。我之前的成功部分是因为我可以使用 [tableview reloadData] 调用来指示可以在缓存图像后显示单元格缩略图。我如何对以编程方式创建的表视图实现延迟加载?我最初的想法是在表格视图中添加某种选择器,以便在为特定单元格缓存图像后更新单元格。我不确定这将如何工作,并希望得到任何帮助。
以下是我用来以编程方式创建表视图的代码(我使用的是 ARC):
@interface SyllabusSegmentedViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, weak)IBOutlet UIScrollView *SyllabusSegmentedSV;
--
@implementation SyllabusSegmentedViewController
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Programmatic creation of the table views and titles for Table Views
[self ProgramaticCreationOfTableView:self];
}
-(void) ProgramaticCreationOfTableView:(id)sender {
for (numTableViews = 0; numTableViews < [syllabusDispayed.LevelNames count]; numTableViews ++) {
// Create Table View
UITableView *newTableView = [[UITableView alloc] init];
newTableView = [self createTableView:self];
// Create UILabel that will act as Title for Each table view
UILabel *newLabel = [[UILabel alloc] initWithFrame:[self MakeHeaderLabelFrame:numTableViews workingTableView:newTableView]];
[newLabel setBackgroundColor:nil];
newLabel.backgroundColor = [UIColor colorWithRed:(204/255.0) green:(204/255.0) blue:(204/255.0) alpha:1.0];
newLabel.text = [syllabusDispayed.LevelNames objectAtIndex:numTableViews];
newLabel.textAlignment = NSTextAlignmentCenter;
UIFont *font = [UIFont boldSystemFontOfSize:20];
UIColor *color = [UIColor darkGrayColor];
newLabel.font = font;
newLabel.textColor = color;
[newTableView setBackgroundView:nil];
newTableView.backgroundColor = [UIColor colorWithRed:(204/255.0) green:(204/255.0) blue:(204/255.0) alpha:1.0];
newTableView.scrollEnabled = NO;
newTableView.frame = ([self MakeTableViewFrame:numTableViews workingTableView:newTableView]);
newTableView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
newLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[SyllabusSegmentedSV addSubview:newTableView];
[SyllabusSegmentedSV addSubview:newLabel];
}
}
-(UITableView *)createTableView:(id)sender {
UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStyleGrouped];
tableView.delegate = self;
tableView.dataSource = self;
tableView.accessibilityLabel = [NSString stringWithFormat:@"%d", numTableViews];
return tableView;
}
-(CGRect)MakeHeaderLabelFrame:(int)workingTableNumber workingTableView:(UITableView *)newTableView {
int numberOfPreviousLevelsRows = 0;
for (int i = 0; i < workingTableNumber ; i ++) {
numberOfPreviousLevelsRows += [[syllabusDispayed.SyllabusDictionary objectForKey:[syllabusDispayed.LevelNames objectAtIndex:i]] count];
}
return CGRectMake(0, 75 + ((workingTableNumber * 80) + (numberOfPreviousLevelsRows *55)), self.view.bounds.size.width, 25);
}
-(CGRect)MakeTableViewFrame:(int)workingTableNumber workingTableView:(UITableView *)newTableView {
int numberOfPreviousLevelsRows = 0;
for (int i = 0; i < workingTableNumber ; i ++) {
numberOfPreviousLevelsRows += [[syllabusDispayed.SyllabusDictionary objectForKey:[syllabusDispayed.LevelNames objectAtIndex:i]] count];
}
return CGRectMake(0, 95 + ((workingTableNumber * 80) + (numberOfPreviousLevelsRows *55)), self.view.bounds.size.width, 65*[newTableView numberOfRowsInSection:0]);
}
我还为 UITableViewDelegate 实现了必要的功能,例如
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath