2014-08-29 39 views
3

我已經用盡了Google上的這個,儘管我確信這是一件相當令人尷尬的事情。viewForHeaderInSection在UITableView中僅被調用一次

我想創建一個UITableView代表25個(比方說)對象的25個部分中的每一行。

我創建的自定義單元格顯示正常(全部25個,按正確的順序)。我想要在每個對象上方顯示節標題視圖。

問題是僅顯示一個節標題視圖(第一個)。

明智的NSLogging告訴我viewForHeaderInSection只被調用一次,儘管heightForHeaderInSection被稱爲每個對象2次。我從numberOfSectionsInTableView返回25,從numberOfRowsInSection返回1。

我正在通過做UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, viewHeight)]然後添加一個UILabel和一個UIButton在viewForHeaderInSection構造一個UIView。我不是繼承UITableViewHeaderFooterView。不知道這是否有所作爲。

任何想法?

相關的代碼:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [self.objects count]; 
} 

- (NSInteger)numberOfRowsInSection:(NSInteger)section 
{ 
    return 1; 
} 

身高和細胞的行:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 390; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
         object:(PFObject *)object 
{ 
    static NSString *CellIdentifier = @"CustomCell";  
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    // add custom content to cell 

    return cell; 
} 

高度和視圖集管:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 60; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    NSLog(@"in viewForHeaderInSection %ld", (long)section); // I only see this once 
    NSLog(@"return view for section %ld", section);   // this is 0 when I see it 

    // gather content for header 
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, viewHeight)]; 
    // add content to header (displays correctly) 

    return headerView; 
} 

回答

0

部和行數這不是回答這個問題,但是當我正在格式化上述內容時,出現了這個問題的解決方案:如果每個單元格都需要一個頭文件,只需將頭文件內容構建到單元格中,而不是試圖將其放在單獨的頭文件視圖中。嘆...

但仍然有興趣知道爲什麼viewForHeaderInSection似乎只被調用一次,但。

+0

你真的應該dequeing表視圖章節標題像你這樣做的細胞。基本上,你已經爲25個不同的部分創建了一個UIView實例....如果你想快速使用它,可以使用titleForSection:方法,然後你會看到你是否至少得到了正確的切片。但是,故事的寓意是像單元格一樣正確地將表格視圖標題出列,並且它應該起作用。 – DBoyer 2014-08-29 08:23:09

-1

添加此委託方法

- (CGFloat) tableView: (UITableView*) tableView heightForFooterInSection: (NSInteger) section 
2

雖然爲時已晚。經過30分鐘的戰鬥,我在xCode 8.1中找到了一個解決方案。我正在把這可能有助於其他。

//添加波紋管委託方法後,我發現這個所謂的多次

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 30.0; 
} 
相關問題