簡體   English   中英

如何在WatchKit tableview中重新加載數據?

[英]How can I reload the data in a WatchKit tableview?

我有一個帶有Tableview的iPhone應用程序,其數據來自CoreData。

在此輸入圖像描述

相同的數據也會顯示在觀看應用中:

在此輸入圖像描述

如果我從iPhone應用程序中添加一行:

在此輸入圖像描述

我在Watch應用程序中重新加載數據:

在此輸入圖像描述

我看到舊行空了!

在此輸入圖像描述

如果停止手表應用程序並重新啟動它,一切都會正確顯示!

在此輸入圖像描述

這是在Watch應用程序中填充Tableview的代碼

-(void)awakeWithContext:(id)context{
    [super awakeWithContext:context];
       [self loadTable];
}

-(void)loadTable{
    NSLog(@"loadTableData");
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Data"];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Data"
        inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sortByDate = [[NSSortDescriptor alloc] initWithKey:@"sortedDateAndTime" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortByDate, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    self.watchMArray = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

  //  [self.watchTableView setRowTypes:self.watchMArray];
    [self.watchTableView setNumberOfRows:self.watchMArray.count withRowType:@"data"];

    for (NSInteger i = 0; i < self.watchMArray.count; i++)
    {
        WatchTableCell *cell = [self.watchTableView rowControllerAtIndex:i];
        NSManagedObject *data = [self.watchMArray objectAtIndex:i];
        dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
            //Background Thread
            UIImage *foto =[self loadImageFromData:[data valueForKey:@"imageData"]];
            dispatch_async(dispatch_get_main_queue(), ^(void){
                //Run UI Updates
            [cell.watchImage setImage:foto];
            [cell.watchDate setText:[NSString stringWithFormat:@"%@", [data valueForKey:@"dataEOra"] ]];
            });
        });
    }
}

這是我目前用來重新加載它的代碼:

- (IBAction)reloadTable {        
    [self loadTable];
}

我哪里錯了?

在@leolobato發布的鏈接中,實際上有一種解決方法。 它確實解決了我的問題。

https://devforums.apple.com/message/1098875#1098875

當你改變行時,首先將它們設置為= @“”。

例如,如果你的行有一個row.Label並且你要更改它,那么在row.Label.text = @“[Actual text]”之后再次執行row.Label.text = @“”

調用setRowTypes:setNumberOfRows:withRowType:方法。

以下是開發人員文檔中的聲明,我相信這會起作用。

如果要更新表的內容,請使用新的行類型信息再次調用setRowTypes:或setNumberOfRows:withRowType :. 再次調用這些方法會強制表丟棄舊行並創建新行。 要在不刪除舊行的情況下插入新行,請使用insertRowsAtIndexes:withRowType:方法。

鏈接到文檔

我發現調用setNumberOfRows:rowType不刷新表,除非你從willActivate方法調用它。 我有一個委托方法,我從一個模態控制器調用,它將從父應用程序中獲取一組新的記錄,然后重新加載表。 即使打印出的numberOfRows顯示了我設置的新行數,但在視覺上現有的行仍顯示在屏幕上。 只有在我的委托方法中設置了一個標志,然后在willActivate中檢查了該標志,然后才重新加載表,它是否刷新了顯示。

要重新加載數據,請將您的IBAction更改為:

- (IBAction)reloadTable {
    [self.watchTableView setNumberOfRows:self.watchMArray.count withRowType:@"data"];
}

來自Apple關於WKInterfaceTable的開發人員文檔

此方法從表中刪除任何現有行,並根據numberOfRows和rowType參數中的信息配置一組新行。

鑒於您在原始代碼中使用setNumberOfRows:withRowType:我在此處使用了它。 方法setRowTypes:也達到了相同的效果。

對於watchOS 5.1,iOS 12.1,只有在didAppear方法中添加了setNumberOfRows:withRowType: didAppear使用它。 使用awake(withContext:)willActivate對我不起作用。 前4行是空白的。

我相信你必須同時做兩件事才能達到tableview.reloadData()iOS函數的相同結果,所以要用新數據重新加載表中的表,執行以下操作:1-call table method:setNumberOfRows 2-loop通過行刷新值。

vacationList.setNumberOfRows(dict.count, withRowType: "vacationRow")
    for index in 0..<vacationList.numberOfRows {
        if let controller = vacationList.rowControllerAtIndex(index) as? VacationRowcontroller {
            controller.vacation = dict.objectForKey("\(index)") as? NSDictionary
        }
    } 

提示:dict =來自后端的新重載數據的字典。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM