1

我正在编写一个 iOS Today Extension,UITableView里面有一个。我的想法是在加载扩展时从文件中加载存档数据,在 UI 上显示数据,并在widgetPerformUpdateWithCompletionHandler.

获取远程内容后,将保存在本地存档文件中,并调用reloadData. 也回应UITableView。当用户点击其中一行时,它将启动 Safari。UITableViewtableView:didSelectRowAtIndexPath

但我注意到,当加载扩展时,系统将使用以前的快照而不是显示我的 UI。很好,我知道这是出于性能原因。但是快照不能像这样处理任何 UI 事件UITableView,这意味着用户在看到扩展程序后会有延迟,直到他们可以点击每一行。

有谁知道如何减少快照可见的时间?widgetPerformUpdateWithCompletionHandler加载扩展时,似乎 Today 没有调用for 扩展。

这是我正在使用的示例代码:

@implementation TodayViewController

- (void)viewDidLoad {
    [super viewDidLoad];     
    self.preferredContentSize = CGSizeMake(0, 0);
    [self loadArchivedData];
    self.preferredContentSize = CGSizeMake(0, self.stories.count * kCellHeight);

    [self.tableView registerNib:[UINib nibWithNibName:@"RHNTodayCell" bundle:nil] forCellReuseIdentifier:@"RHNTodayCell"];
}

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.tableView reloadData];
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.stories.count;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    RHNTodayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RHNTodayCell" forIndexPath:indexPath];
    NSDictionary *story = self.stories[indexPath.row];
    cell.titleLabel.text = story[@"title"];
    cell.scoreLabel.text = [NSString stringWithFormat:@"%d", (int)[story[@"score"] integerValue]];
    cell.commentLabel.text = [NSString stringWithFormat:@"%d", (int)[story[@"kids"] count]];
    cell.nameLabel.text = story[@"by"];

    return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSDictionary *story = self.stories[indexPath.row];
    [self.extensionContext openURL:[NSURL URLWithString:story[@"url"]] completionHandler:nil];
}

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

    completionHandler(NCUpdateResultNewData);
    [[RemoteAPI sharedClient] asyncFetchRemoteData:(NSArray *response) {
            [self saveArchiveFilesWithResponse: response];
            [self loadArchivedData];
            self.preferredContentSize = CGSizeMake(0, self.stories.count * kCellHeight);
            [self.tableView reloadData];
            completionHandler(NCUpdateResultNewData);
    }];
}

@end
4

0 回答 0