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