2014-11-22 62 views
3

我刚开始学习如何在iOS8上创建今日视图窗口小部件。我为扩展创建了一个新的目标。下一步,我删除了默认viewcontroller和类,并创建了一个新的UITableViewController,它是相应的方法。我实现了以下内容:今天的iOS8视图窗口小部件不显示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; 
} 

我也改变了表的大小来freeform它的高度设置为300像素。但我没有得到任何回报。小部件变空了。

回答

10

最有可能的原因是,您今天的扩展需要通过为self.preferredContentSize指定一个值来指定它的UI需要多少空间。在你的情况下,这是一个高度(表格行数)*(每行高度),无论通知中心给你什么宽度。在你viewDidLoad你需要的东西是这样的:

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

,它假定的44单元高度,它看起来像它在你的情况下正确的。

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

+0

非常感谢汤姆!非常精确和有用的答案。 – user3687 2014-11-25 18:38:55