Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS8 today view widget not showing UITableViewController

I have just started learning how to create today view widgets on iOS8. I created a new target for the extension. Next step i deleted the default viewcontroller and classes and created a new UITableViewController and it's corresponding methods. I implemented the following:

#import "TableViewController.h"
#import <NotificationCenter/NotificationCenter.h>

   @interface TableViewController () <NCWidgetProviding>
{
    NSArray *nameArray;
}
@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    nameArray = [[NSArray alloc]initWithObjects:@"joey",@"jack",@"jill", nil];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {


    completionHandler(NCUpdateResultNewData);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    cell.textLabel.text = [nameArray objectAtIndex:indexPath.row];

    return cell;
}

I have also changed the table size to freeform and set its height to 300px. But i get nothing in return. The widget comes up empty.

like image 541
Rashid Avatar asked Dec 20 '22 09:12

Rashid


1 Answers

The most likely reason is that your today extension needs to specify how much space it needs for its UI by assigning a value to self.preferredContentSize. In your case this is a height of (number of table rows) * (height per row) with a width of whatever the notification center gives you. In your viewDidLoad you need something like this:

self.preferredContentSize = CGSizeMake(self.preferredContentSize.width, nameArray.count * 44.0);

That assumes a cell height of 44, which looks like it's correct in your case.

If the size of the array ever changes, you need to assign a new value to preferredContentSize.

like image 186
Tom Harrington Avatar answered Dec 24 '22 01:12

Tom Harrington