Andrew Hart
Andrew Hart

Reputation: 91

Lazy Load cell image for programmatically created tableview

I am creating a number of tableviews programmatically. The client has specifically asked for a layout that is not a UITableView Controller. Something like:

enter image description here

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.

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]);
 }

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 

Upvotes: 2

Views: 1259

Answers (1)

Mikhail
Mikhail

Reputation: 4311

Take a look at library SDWebImage

I think this is what you are looking for.

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.

EDIT

TableViewDataSource has method named – numberOfSectionsInTableView:.

Also by implementing – tableView:heightForHeaderInSection: or – tableView:heightForFooterInSection: you can get necessary space between sections.

You should implement all this methods in SyllabusSegmentedViewController.

Upvotes: 1

Related Questions