2014-05-06 73 views
11

我的UITableView在每個部分都有自定義的UIView標題。我只需要刷新標題而不是本節中的其他內容。我試過了[self.tableView headerViewForSection:i],即使它應該也不會返回任何東西。無論如何,我可以做到這一點?只刷新UITableView中的自定義標題視圖?

編輯:根據各地新建議

我給這個一杆以及代碼和它調用/該方法中更新的UIView,但變化不會在視覺傳播到屏幕上。

for (int i = 0; i < self.objects.count; i++) { 
    UIView *headerView = [self tableView:self.tableView viewForHeaderInSection:i]; 
    [headerView setNeedsDisplay]; 
} 
+0

你使用一個筆尖加載你的頭,或者你是否在'tableView:viewForHeaderInSection:'中以編程方式創建它們? – Stonz2

+0

我正在以規定的方法編程創建它們。 – Jonathan

回答

12

而不是調用setNeedsDisplay,通過設置它的屬性來自己配置標題。當然,你必須得到表中的實際標題,不要調用委託方法,因爲該方法通常會創建一個新的標題視圖。

我通常在一個小幫助方法中執行此操作,該方法也從tableView:viewForHeaderInSection:中調用。

例如爲:

- (void)configureHeader:(UITableViewHeaderFooterView *)header forSection:(NSInteger)section { 
    // configure your header 
    header.textLabel.text = ... 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"Header"]; 
    [self configureHeader:header forSection:section]; 
} 

- (void)reloadHeaders { 
    for (NSInteger i = 0; i < [self numberOfSectionsInTableView:self.tableView]; i++) { 
     UITableViewHeaderFooterView *header = [self.tableView headerViewForSection:i]; 
     [self configureHeader:header forSection:i]; 
    } 
} 
+1

所有這些工作都很好,但是調用UITableViewHeaderFooterView * header = [self.tableView headerViewForSection:i];'不會返回任何東西。我試着做'[self tableView:self.tableView viewForHeaderInSection:i]'並且它返回一個頭文件並帶我進行配置,但仍不會在屏幕上直觀地顯示這些更改。 – Jonathan

+2

if'[self。tableView headerViewForSection:i]'返回nil,那麼你的tableView沒有這個部分的頭部,或者它目前不可見。當然,'[self tableView:self.tableView viewForHeaderInSection:i]'會返回一個視圖,但這個視圖是新創建的,不屬於視圖層次結構的一部分;你看不到那個觀點。 –

+0

我能做些什麼來檢查?在視覺上,表格視圖爲每個部分設置了標題,並且它們正按照上面的方式進行實例化。 – Jonathan

0

創建自己的UIView爲headerView的子類,並添加了幾個方法,它使您的視圖控制器可以發送你想它的任何UI更新。

0

當您創建自定義標題視圖,請使用UITableViewHeaderFooterView,而不是一個簡單的UIView當你初始化它,使用這樣的:

UITableViewHeaderFooterView *cell = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"reuseIdentifier"]; 

reuseIdentifier會告訴你UITableView來跟蹤頭和它不應該再回到null你試圖用[self.tableView headerViewForSection:i]

訪問它,那麼你應該能夠上使用setNeedsDisplayUITableViewHeaderFooterView你回來後進行修改。

0

調用setNeedsDisplay僅用於觸發接收視圖的方法drawRect:。除非這是你的文本顏色邏輯(也就是說,你用於標題視圖的UIView子類的drawRect方法),否則沒有什麼可能改變。您需要做的是直接調用標題視圖上的某些內容,根據新的或更改的數據更新其狀態,或者直接訪問標題視圖中的標籤,並在遍歷它們時自行更改它。 setNeedsDisplay只會觸發在視圖的drawRect:方法中找到的代碼。

0

最近,我有這個確切的問題,只有通過保持一個指向標頭,創建它們克服它。我在Swift中做了這個,只用了一個快速的字典類型,但這個想法可能會以某種方式被按到objC中。

var headers : [ Int : UIView ] = [ : ] //dictionary for keeping pointer to headers 

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

     if let h = super.tableView(tableView , viewForHeaderInSection: section) { //or create your view (this viewController inherits it).. 
     self.headers.updateValue(h , forKey: section) //keep a pointer to it before you return it.. 
     return h 
     } 
     return nil 
    }