今天的iOS8视图小部件没有显示UITableViewController

Ras*_*hid 4 ios ios8-today-widget

我刚开始学习如何在iOS8上创建今天的视图小部件.我为扩展创建了一个新目标.下一步我删除了默认viewcontroller和类,并创建了一个新的UITableViewController及其相应的方法.我实现了以下内容:

#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;
}
Run Code Online (Sandbox Code Playgroud)

我还将表格大小更改为freeform并将其高度设置为300px.但我得不到任何回报.小部件出现空白.

Tom*_*ton 11

最可能的原因是您的今天扩展需要通过为其分配值来指定其UI所需的空间self.preferredContentSize.在您的情况下,这是(表行数)*(每行的高度)的高度,宽度为通知中心提供的任何宽度.在你的viewDidLoad,你需要的东西是这样的:

self.preferredContentSize = CGSizeMake(self.preferredContentSize.width, nameArray.count * 44.0);
Run Code Online (Sandbox Code Playgroud)

假设单元格高度为44,在您的情况下看起来是正确的.

如果数组的大小发生变化,则需要为其分配新值preferredContentSize.