2012-06-05 64 views
0

我有一個視圖控制器,許多視圖和一個tableview。不能自動更新tableview

tableview的單元格是自定義的,所以還有另外一個類來設置單元格。 在每個單元格中都有一個按鈕。此按鈕的圖像根據單元格的內容而變化(從數據庫讀取此內容)。

基本上,當用戶按下按鈕時,它將自己改變爲另一個圖像,新的狀態被寫入數據庫,但tableview不會自動更新自身。

該按鈕的方法是在自定義細胞類,所以我試圖實例我的視圖控制器(所述一個與所述的tableview),並執行用於更新在視圖中的一些標籤和tableview中的方法:

ViewControllerWithTable *vc = [[ViewControllerWithTable alloc] init]; 
[vc updateEverything]; 

但這不起作用。 從相同的「ViewControllerWithTable」調用(添加重載按鈕)的相同「updateEverything」方法完美地工作。 在viewWillAppear方法中添加「[tableView reloadData]」將不起作用,因爲所有操作都在同一視圖中完成。

我錯過了什麼?

編輯:添加一些代碼更清晰。

這是我用來更新tableview的方法。這是與嵌入式的tableview的視圖控制器內,當通過一個按鈕在一個視圖中觸發它的工作原理:

- (void) updateEverything { 
    // lots of DB writing and reading, plus label text changing inside all the views 
    [tableView reloadData]; 

} 

這是IBAction爲按按鍵,它的自定義單元格類:

-(void) btnPresaPressed:(UIButton *)sender { 
    AppDelegate *deleg = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    deleg.did = sender.tag; 
    NSString *s1 = NSLocalizedString(@"ALERT_TITLE", nil); 
    NSString *s2 = NSLocalizedString(@"ALERT_BODY", nil); 
    NSString *s3 = NSLocalizedString(@"YES", nil); 
    NSString *s4 = NSLocalizedString(@"NO", nil); 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:s1 
                 message:s2 
                 delegate:self 
               cancelButtonTitle:s4 
               otherButtonTitles:s3, nil]; 
    [alertView setTag:1]; 
    [alertView show]; 

} 

這種方法表明,調用另一個方法,總是在自定義單元格級警報視圖:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
    AppDelegate *deleg = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    DbOperations *db = [[DbOperations alloc] init]; 
    NSString *alrtTitle = [alertView buttonTitleAtIndex:buttonIndex]; 
    NSString *s3 = NSLocalizedString(@"YES", nil); 
    NSString *s4 = NSLocalizedString(@"NO", nil); 
    switch (alertView.tag) { 
     case 1: 
      // 
      break; 
     case 2: 
      if ([alrtTitle isEqualToString:s3]) { 
       // DB writing and reading 
       ViewControllerWithTable *vc = [[ViewControllerWithTable alloc] init]; 
       [vc updateEverything]; 
      } else if ([alrtTitle isEqualToString:s4]){ 
       // 

      } 
      break; 
     case 3: 
      if ([alrtTitle isEqualToString:s3]) { 
       // 
      } 
      break; 
     default: 
      break; 
    } 

} 

在這種情況下,updateEverything方法不起作用。

+0

那麼'updateEverything'是怎麼樣的呢?有一些方法可以在單獨的indexPath上重新加載單元格。你應該看看它們。 – Eimantas

回答

3

編輯你添加更多的代碼後:

在下面幾行:

 if ([alrtTitle isEqualToString:s3]) { 
      // DB writing and reading 
      ViewControllerWithTable *vc = [[ViewControllerWithTable alloc] init]; 
      [vc updateEverything]; 

你實例完全新的視圖控制器,無關與所顯示的表視圖原始視圖控制器。所以,你正在發送更新消息給錯誤的對象。

你需要的是一個讓你的細胞知道哪個是正確的控制器發送消息的機制。

一個簡單的解決辦法是使用NSNotificationCenter

  1. 視圖控制器寄存器本身在一定形式的通知:

    [[NSNotificationCenter defaultCenter] 
    addObserver:self 
    selector:@selector(updateEverything:) 
    name:kCellSentUpdateMessageNotification 
    object:nil]; 
    
  2. 您的小區發送的通知,而不是調用消息直接:

    [[NSNotificationCenter defaultCenter] postNotificationName:kCellSentUpdateMessageNotification object:nil]; 
    

OLD答:

你應該叫

[self.tableView reloadData] 

updateEverything方法實現。這將重新加載表數據,有效地更新其行外觀。當點擊連續的按鈕以使其工作時,將調用updateEverything方法,顯然。

如果這不起作用,請提供更多代碼。

+0

是的,我已經這樣做了,但是從自定義單元類中調用該方法將不會更新tableview。 – Aleph72

+0

請說明你是如何在代碼中做到這一點的。視圖控制器是否引用了您要從單元發送'updateEverything'消息而不是零? – sergio

+0

我已經添加了一些代碼。請致電 – Aleph72

0

你有沒有嘗試把

[[self tableView] reloadData];

我記得我有這樣的一個問題,而這條線之上解決我的問題。

0

這是你的問題:

該按鈕的方法是在自定義單元格類,所以我試圖 實例我的視圖控制器(在一個與實現代碼如下),並 執行的方法更新視圖中的一些標籤和 實現代碼如下:

您創建了沒有你的tableview的知識,一個全新的視圖控制器。最好的辦法是在您的單元格子類中創建一個屬性,並在設置單元格時將其設置爲您的視圖控制器。然後你可以在該屬性上調用你的updateEverything方法。

+0

我不知道我明白你在說什麼。你能舉個例子嗎? – Aleph72