iphone - 为什么tableViewW :viewForHeaderInSection ignore the frame property of my UILabel?

标签 iphone uitableview frame

基本上,我想更改节标题的字体和颜色,因此我实现了 tableVieW:viewForHeaderInSection。首先我尝试了这段代码:

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel* headerLabel = [[[UILabel alloc] init] autorelease];
    headerLabel.frame = CGRectMake(10, 0, 300, 40);
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.textColor = [UIColor blackColor];
    headerLabel.font = [UIFont boldSystemFontOfSize:18];
    headerLabel.text = @"My section header";

    return headerLabel;
}

但由于某种原因,frame 属性被忽略(我说的是左侧的 10px 插图)。现在我使用以下内容:

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView* headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)] autorelease];

    UILabel* headerLabel = [[UILabel alloc] init];
    headerLabel.frame = CGRectMake(10, 0, 300, 40);
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.textColor = [UIColor blackColor];
    headerLabel.font = [UIFont boldSystemFontOfSize:18];
    headerLabel.text = @"My section header";

    [headerView addSubview:headerLabel];
    [headerLabel release];

    return headerView;
}

得到了想要的结果。有人可以向我解释为什么第二种方法有效而第一种方法无效吗?

PS。在这两种情况下,我也实现了 tableView:heightForHeaderInSection ,返回 40.0

最佳答案

这是因为 UITableView 会自动设置您提供的标题 View 的框架

(0,y,表格 View 宽度,标题 View 高度)

y 是 View 的计算位置, header view heighttableView:heightForHeaderInSection: 返回的值

关于iphone - 为什么tableViewW :viewForHeaderInSection ignore the frame property of my UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4970255/

相关文章:

iphone - 如何在不变形图像的情况下旋转UIImageView?

ios - 无法在 Swift 中将类型 'UITableViewCell' 的值转换为 'NSIndexPath'

java - 如何从 android Surfaceview 中的视频获取当前帧?

ios - 处理 UILabel 中的触摸事件并将其连接到 IBAction

iphone - 使用单点触控的推送通知服务

ios - UITableView/indexPath.row : why does if statement exclude row 10?

ios - UITableView 设置不同的图片

video - 使用 FFMPEG 将帧插入视频

ios - 有没有办法在该对象的动画期间获取 UIImageView 位置?

iphone - objective-c - 重大位置变化