2014-04-02 23 views
0

嗨,大家好,我有以下代碼:更新headerView在didUpdateLocation

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    static NSString *LocationHeaderCellIdentifier = @"Location Header Cell"; 

    LocationHeaderCell *locationHeaderCell; 
    locationHeaderCell = (LocationHeaderCell *)[tableView dequeueReusableCellWithIdentifier:LocationHeaderCellIdentifier]; 
    locationHeaderCell.lblCurrentAddress.text = self.currentAddress; 

    return locationHeaderCell; 
} 

正如你們所看到的,我使用的原型細胞是我的頭。 我想完成的是在didUpdateLocation委託中訪問此標題來更改標籤(刷新它)。我不想使用[reload data],因爲我從不同的Web服務器收到兩個不同的響應。一個來自Parse,另一個來自Apple。因此我不知道哪一個會先到。 我嘗試檢索標題視圖並打印Null。

有什麼建議嗎?

回答

0

你可以保持這個LocationHeaderCell參考的屬性,例如:

@property(nonatomic, strong) LocationHeaderCell *myHeader; 

和修改這樣的代碼:

if (self.myHeader==nil) 
    self.myHeader = (LocationHeaderCell *)[tableView dequeueReusableCellWithIdentifier:LocationHeaderCellIdentifier]; 

所以在didUpdateLocation只是做一個self.myHeader.lblCurrentAddress.text = @whatever";

+0

但我不知道這是否是一種好的做法。 – Camus

+0

所以我保留一個參考,它正在工作。然而,我不明白的是爲什麼如果我寫'(LocationHeaderCell *)[tableView dequeueReusableCellWithIdentifier:LocationHeaderCellIdentifier];'它給我帶來了零。 – Camus

+0

它帶給你零,因爲沒有可以出隊的單元格。 dequeueReusableCellWithIdentifier總是爲您提供一個未使用的單元格(例如,在屏幕外),如果存在的話。您將永遠不會從此方法獲取當前正在屏幕上顯示的單元格。如果您保留對有限數量的單元格的引用(例如標題單元格),那麼這是一個好習慣。無論如何,可見單元格保存在內存中(並且被引用)。 – gWiz

相關問題