更改页脚宽度的部分不起作用

时间:2014-02-17 16:23:40

标签: ios objective-c uitableview

嘿,我尝试使用以下部分更改页脚的宽度:

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

UIView *viewFooter = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 300, 44)];

[viewFooter setBackgroundColor:[UIColor blueColor]];

return viewFooter;
}

但是页脚总是和tableview一样宽(320.0f).....

2 个答案:

答案 0 :(得分:3)

是的,表格视图将始终使该部分成为表格的整个宽度。

最简单的解决方案是让您的“真实”页脚成为实际页脚视图的子视图。

这样的事情:

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *mainFooter = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

    UIView *viewFooter = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 300, 44)];
    [viewFooter setBackgroundColor:[UIColor blueColor]];
    viewFooter.autoresizingMask = UIViewAutoresizingMaskFlexibleWidth;
    [mainFooter addSubview:viewFooter];

    return mainFooter;
}

// You must implement this when you implement viewForFooterInSection
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 44;
}

答案 1 :(得分:1)

如果容器的大小与实际的tableview不同,则需要在所需的页脚自定义视图周围添加容器。这应该有效:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

    UIView *viewFooter = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 300, 44)];
    [viewFooter setBackgroundColor:[UIColor blueColor]];


    // Create a footer container that has a fixed width
    // but in which you can adjust subviews frame as you want.
    //
    UIView *footerContainer = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 320, 44)];
    [footerContainer addSubview:viewFooter];

    return footerContainer;
}