懒惰的称重传感器图像,用于以编程方式创建的tableview [英] Lazy Load cell image for programmatically created tableview
问题描述
我正在以编程方式创建许多表格视图.客户端专门要求的布局不是UITableView Controller.像这样:
I am creating a number of tableviews programmatically. The client has specifically asked for a layout that is not a UITableView Controller. Something like:
表视图的数量以及每个表中的项目数都不同.视图加载时立即创建所有表.我想在表格视图的单元格中添加缩略图.我需要找到一种方法来延迟加载缩略图.我已经在UITableViewController上成功实现了延迟加载.我之前的成功部分是因为我可以使用[tableview reloadData]调用指示在缓存图像之后可以显示单元格缩略图.我如何实现以编程方式创建的表视图的延迟加载?我最初的想法是在表视图中添加某种选择器,以便在为特定单元格缓存了图像后更新单元格.我不确定这将如何工作,将不胜感激.
The number of tableviews as well as the number of items in each table varies. All of the tables are created at once when the view loads. I would like to add thumbnails to the cells in the table views. I need find a way to lazy load the thumbnails. I have successfully implemented a lazy load on a UITableViewController. My prior success is in part because I can use the [tableview reloadData] call to indicate the cell thumbnails can be displayed after the images have been cached. How might I implement a lazy load to the tableviews created programmatically? My initial idea is to add some sort of selector to the table view in order to update a cell once the images have been cached for a particular cell. I am not sure how this would work and would appreciate any help.
以下是我用于以编程方式(我正在使用ARC)创建表视图的代码:
The following is the code that I use to create the table views programmatically (I am using 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实现了必要的功能,例如
I also have implemented the necessary function for a UITableViewDelegate such as
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
推荐答案
看看库 SDWebImage
我认为这就是您想要的.
I think this is what you are looking for.
但是这可能是您的方法的真正问题:我想实际上并不需要创建多个TableView并排放置的TableViews-在这种情况下,各节可能会做这件事.
But may be the real problem with your approach: i guess that it is not really necessary to create multiple TableViews that located one after another - it's case where sections may do the thing.
编辑
TableViewDataSource具有名为– numberOfSectionsInTableView:
的方法.
TableViewDataSource has method named – numberOfSectionsInTableView:
.
还可以通过实现– tableView:heightForHeaderInSection:
或– tableView:heightForFooterInSection:
来获得各节之间的必要空间.
Also by implementing – tableView:heightForHeaderInSection:
or – tableView:heightForFooterInSection:
you can get necessary space between sections.
您应该在SyllabusSegmentedViewController中实现所有这些方法.
You should implement all this methods in SyllabusSegmentedViewController.
这篇关于懒惰的称重传感器图像,用于以编程方式创建的tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!