wal*_*ace 5 iphone objective-c uitableview ios
虽然我确信这是一件非常简单的事情,但我已经筋疲力尽了谷歌.
我正在尝试创建一个UITableView,表示25个(比方说)对象,每个对应25个部分,每行1行.
我正在创建的自定义单元格显示正常(所有25个,按正确顺序).我想在每个对象上方显示一个节头视图.
问题是只显示了一个节标题视图(第一个).
明智的NSLogging告诉我,只有一次调用viewForHeaderInSection,尽管每个对象调用heightForHeaderInSection 2x.我从numberOfSectionsInTableView返回25,从numberOfRowsInSection返回1.
我在viewForHeaderInSection中构建一个UIView,UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, viewHeight)]
然后添加一个UILabel和一个UIButton.我不是UITableViewHeaderFooterView的子类.不确定这是否有所不同.
有任何想法吗?
相关代码:
部分和行数:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.objects count];
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
return 1;
}
Run Code Online (Sandbox Code Playgroud)
行的高度和单元格:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
标题的高度和视图:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
虽然为时已晚.经过30分钟的战斗,我在xCode 8.1中找到了解决方案.我把它放在其他可能有帮助的地方.
//添加了波纹管委托方法后,我发现这个被多次调用
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
return 30.0;
}
Run Code Online (Sandbox Code Playgroud)
这并没有回答问题,但是当我格式化上面的内容时,我突然想到了这个问题的解决方案:如果每个单元格都需要标题,只需将标题内容构建到单元格中,而不是尝试将其放在单独的单元格中标题视图。叹...
不过,仍然有兴趣知道为什么viewForHeaderInSection
似乎只被调用一次。
归档时间: |
|
查看次数: |
1516 次 |
最近记录: |