2009-04-30 53 views
22

我有一個具有UITableView的應用程序。這個UITableView被一個NSMutableArray填充(作爲一個屬性)在appDelegate中。您可以將其視爲電子郵件窗口。它在子類UITableViewCell中列出消息。當出現一條新消息時,我會完成所有下載消息的代碼,並將數據添加到包含所有消息的appDelegate的NSMutableArray中。此代碼工作正常。什麼是[UITableView reloadData]?

現在,一旦新消息被下載並添加到數組中,我試圖用下面的代碼更新我的UITableView,但是 - UITableView的委託函數不會被調用。

奇怪的是,當我向上和向下滾動我的UITableView時,委託方法終於被調用,並且我的節標題發生改變(它們顯示該節的消息計數)。他們不是實時更新,而是不等我滾動觸發刷新?此外,新的細胞永遠不會添加在該部分!

請幫忙!!

的appdelegate CODE:

[self refreshMessagesDisplay]; //This is a call placed in the msg download method 

-(void)refreshMessagesDisplay{ 
    [self performSelectorOnMainThread:@selector(performMessageDisplay) withObject:nil waitUntilDone:NO]; 
} 

-(void)performMessageDisplay{ 
    [myMessagesView refresh]; 
} 

的UITableViewController代碼:

-(void) refresh{ 
    iPhone_PNPAppDelegate *mainDelegate = (iPhone_PNPAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    //self.messages is copied from appDelegate to get (old and) new messages. 
    self.messages=mainDelegate.messages; 

    //Some array manipulation takes place here. 

    [theTable reloadData]; 
    [theTable setNeedsLayout]; //added out of desperation 
    [theTable setNeedsDisplay]; //added out of desperation 
} 
+2

哥們我覺得你的痛苦,在這裏同樣的問題。 – 2011-04-13 06:21:53

回答

29

作爲一個全面的檢查,你驗證theTable不是nil在這一點?

+0

該表不是零,但現在你提到它,它確實顯示爲地址0x0。 IBOutlet theTable鏈接到IB中的控件,並且該表在整個過程中保持可見.... – Dutchie432 2009-04-30 15:29:02

+7

0x0實際上是nil--表視圖當然存在,但是您的指針沒有正確設置。仔細檢查你的筆尖。 – 2009-04-30 16:27:52

+0

那肯定是那個問題。出於某種原因(不可能說沒有看到所有的代碼)你的'theTable'變量是零,所以reloadData消息不會去任何地方。 – smorgan 2009-04-30 16:36:45

0

您是否嘗試過在刷新方法中設置斷點,以確保您的消息數組在調用reloadData之前具有正確的內容?

12

你可以嘗試在reloadData調用中加入延遲 - 當我試圖讓我的tableview在重新排序單元格時進行更新時,我遇到了類似的問題,除非應用程序在我調用reloadData期間崩潰了。

所以這樣的事情可能是值得一試:

刷新方法:

- (void)refreshDisplay:(UITableView *)tableView { 
    [tableView reloadData]; 
} 

,然後與(比如說)0.5秒的延時稱之爲:

[self performSelector:(@selector(refreshDisplay:)) withObject:(tableView) afterDelay:0.5]; 

希望它作品...

6

如果您從調度方法中調用reloadData,請確保執行它o主隊列。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void) { 

    // hard work/updating here 

    // when finished ... 
    dispatch_async(dispatch_get_main_queue(), ^(void) { 
     [self.myTableView reloadData]; 
    }); 
}); 

..same的方法形式:

-(void)updateDataInBackground { 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void) { 

     // hard work/updating here 

     // when finished ... 
     [self reloadTable]; 
    }); 
} 

-(void)reloadTable { 
     dispatch_async(dispatch_get_main_queue(), ^(void) { 
      [myTableView reloadData]; 
     }); 
}