3

I'm trying to display data in Today Widget extension in UIViewController subclass. The number of items (rows) is alway 3 but the tableView:cellForRowAtIndexPath: is called only once.

I've double-checked everything but cannot find a bug.

Any suggestions? Thanks!

#import "TodayViewController.h"

#import "FLWAccount.h"
#import "FLWAccountTableViewCell.h"
#import "FLWAuthManager.h"

#import <NotificationCenter/NotificationCenter.h>

@interface TodayViewController () <NCWidgetProviding, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, readwrite, weak) IBOutlet UITableView
*tableView;

@end

@implementation TodayViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.estimatedRowHeight = 79.0f;
    self.tableView.rowHeight = 79.0f;

    [self updatePreferredContentSize];
    [self.tableView reloadData];}

- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
    [[FLWAuthManager sharedManager] updateAllAccountsWithCompletion:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [self updatePreferredContentSize];
            [self.tableView reloadData];
            completionHandler(NCUpdateResultNewData);
        });
    }]; }

#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1; }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count = [FLWAuthManager sharedManager].accounts.count;
    return count; }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FLWAccountTableViewCell *cell = (FLWAccountTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FLWAccountTableViewCell" forIndexPath:indexPath];
    cell.account = [FLWAuthManager sharedManager].accounts[indexPath.row];
    return cell; }

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 79.0f; }

- (void)updatePreferredContentSize {
    NSInteger count = [FLWAuthManager sharedManager].accounts.count;
    self.preferredContentSize = CGSizeMake(self.preferredContentSize.width, 79.0f * count); }

@end
Martin Pilch
  • 3,245
  • 3
  • 38
  • 61

1 Answers1

1

If you are using storyboard, check if the table view content is set to "Dynamic Prototypes" (and NOT "Static cells")

enter image description here

Tsahi Deri
  • 571
  • 1
  • 3
  • 14