ios - 以编程方式创建的 TableView 的延迟加载单元格图像

标签 ios objective-c uitableview lazy-loading

我正在以编程方式创建许多表格 View 。客户特别要求不是 UITableView Controller 的布局。像这样的东西:

enter image description here

tableview 的数量以及每个表中的项目数量各不相同。所有的表都是在 View 加载时立即创建的。我想将缩略图添加到表格 View 中的单元格。我需要找到一种延迟加载缩略图的方法。我已经成功地在 UITableViewController 上实现了延迟加载。我之前的成功部分是因为我可以使用 [tableview reloadData] 调用来指示可以在缓存图像后显示单元格缩略图。我如何对以编程方式创建的 TableView 实现延迟加载?我最初的想法是在 TableView 中添加某种选择器,以便在为特定单元格缓存图像后更新单元格。我不确定这将如何工作,并希望得到任何帮助。

以下是我用来以编程方式创建 TableView 的代码(我使用的是 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 

最佳答案

看看图书馆SDWebImage

我认为这就是您要找的。

但这可能是您的方法的真正问题:我想实际上没有必要创建一个接一个地放置的多个 TableView - 在这种情况下,部分可能会做这件事。

编辑

TableViewDataSource 具有名为 – numberOfSectionsInTableView: 的方法。

此外,通过实现 – tableView:heightForHeaderInSection:– tableView:heightForFooterInSection:,您可以获得部分之间的必要空间。

您应该在 SyllabusSegmentedViewController 中实现所有这些方法。

关于ios - 以编程方式创建的 TableView 的延迟加载单元格图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18083866/

相关文章:

iOS Health Kit 临床患者数据

ios - 如何在 Swift2 2.0 中将字符串转换为日期

iphone - 如何在数组中包含 UITableViewCell 引用对象?

ios - 从弹出窗口中的 UISwitch 操作隐藏 UIImageView (iPad)

ios - 如何在iphone中制作类似NSTableView的表格

ios - tableRow 内的 tableView.dequeueReusableCell()(_ :heightForRowAt:) method

objective-c - 从 NSDictionary 访问数据

ios - UIImage initWithContentsOfFile 为空

objective-c - 将 NSTableView 内的复选框设置为 "YES"

objective-c - NSOutlineView 的自动保存扩展项不起作用