IOS8 today view widget not showing UITableViewController
I just started learning how to create today's widgets on iOS8. I created a new target for the extension. In the next step, I removed the default viewcontroller
and classes and created a new one UITableViewController
and its corresponding methods. I did 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 also resized the table to freeform
and set its height to 300px. But I get nothing in return. The widget looks empty.
source to share
The most likely reason is that your today's extension should specify how much space it needs for its UI by assigning a value self.preferredContentSize
. In your case, this is the height (number of table rows) * (height in a row) with the width that the action center provides. In yours, viewDidLoad
you need something like this:
self.preferredContentSize = CGSizeMake(self.preferredContentSize.width, nameArray.count * 44.0);
This assumes the cell height is 44, which looks correct in your case.
If the size of the array ever changes, you need to assign a new value preferredContentSize
.
source to share