带storyboard的自定义tableview section header出现错误



如果我使用storyboard添加自定义uitableviewsectionheader,我得到这个错误AX ERROR: Could not find my mock parent, most likely I am stale.这个错误意味着什么,我如何解决它?头代码:

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *cellID = @"sectionHeaderID";
    UITableViewCell *headerview = [tableView dequeueReusableCellWithIdentifier:cellID];
    return headerview;
}

实际上,如果你看看你链接pedro的帖子中的评论。M给出了一个可能的解。他给单元格添加了一个子视图,他称之为contentView,然后返回它。

我所做的稍微简单一点:

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *cellID = @"sectionHeaderID";
    UITableViewCell *headerview = [tableView dequeueReusableCellWithIdentifier:cellID];
    return headerview.contentView;
}

Section header与表视图单元格不同。你应该为header视图创建你自己的UIView而不是从表格视图单元格中获取它。例如:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerview = [[UIView alloc] initWithFrame:CGRectMake(0,0,CGRectGetWidth(self.view.frame), 100.0)];
    return headerview;
}

相关内容

最新更新