【问题标题】:a trouble about UITableFooterView关于 UITableFooterView 的一个问题
【发布时间】:2023-03-19 10:07:01
【问题描述】:

在tableview中添加footerview后,如果table只有一个cell,footer在cell的底部显示有点高。无论表格有多少个单元格,如何让它显示在表格视图的底部?

【问题讨论】:

  • tablefooterview 的位置取决于有多少节/单元格和单元格的高度,如果您希望 footerview 始终位于 tableview 的底部,我认为只有自定义视图可以帮助您。
  • 那行不通。如果我同时将自定义视图添加到表格视图中,表格中有很多单元格,并且自定义视图将永远不会显示在最后一个单元格下方。

标签: ios objective-c uitableview table-footer


【解决方案1】:

footerView 只是背景,添加一个你真​​正想要的视图,就像这样:

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 100
}

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

    let footerView = UIView.init()
    footerView.backgroundColor = .red

    // footerView is just the background, add a view that you really want
    let subView = UIView.init(frame: CGRect.init(x: 20, y: 20, width: UIScreen.main.bounds.width - 40, height: 100 - 40))
    subView.backgroundColor = .yellow
    footerView.addSubview(subView)

    return footerView
}

【讨论】:

  • 问题标记为 Objective-C。请以适当的语言发布答案。
  • 我以这种方式返回标签​​,不起作用。页脚始终显示在最后一个单元格下方。如果第一个单元格也是最后一个单元格,则页脚显示得有点高。将子视图添加到页脚无法更改其位置。
  • 返回一个有标签的视图。你不能改变'返回视图的框架,但你可以改变标签的位置
【解决方案2】:

这是我在 tableFooterView 上设置视图及其工作正常的代码。

@property (weak, nonatomic) IBOutlet UITableView *tblView;
@property (weak, nonatomic) IBOutlet UIView *viewTableFooterView;

- (void)viewDidLoad {
    [super viewDidLoad];
    _tblView.tableFooterView = _viewTableFooterView;
    _tblView.backgroundColor = [UIColor greenColor];
    [_tblView reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    return cell;
}

检查代码,如果它不适合你,请告诉我。

【讨论】:

    【解决方案3】:

    这是因为您添加的页脚视图是您在UITableView 中显示的内容的页脚。话虽如此,有一种方法可以获得您想要的输出。您可以做的是在故事板中添加的UITableViewCell 下方拖动UIView,使其显示为页脚。

    UILabel 或您想在此视图中显示的任何内容设置约束,我的意思是不要添加固定高度约束,否则您会收到烦人的布局破坏警告。 复制此页脚视图并将其添加到UITableView 的底部,并为其添加一个固定高度,使其成为您在UITableView 中添加的页脚视图的副本。有点像这样:

    现在,为您的UIViewController 中的页脚视图的此高度约束创建一个出口。

    现在在您的viewDidLoad 中,您可以检查添加单元格后UITableView 的高度是否小于屏幕边界并相应地继续,如下所示:

    - (void)viewDidLoad {
    
        [super viewDidLoad];
    
        //dont show footer view if cells are within screen bounds
        if (numberOfCells * cellHeight < [UIScreen mainScreen].bounds.size.height){
    
            UIView *footerView = mainTableView.tableFooterView;
    
            CGRect tempFrame = footerView.frame;
    
            tempFrame.size.height = 0.0f;
    
            footerView.frame = tempFrame;
    
            mainTableView.tableFooterView = footerView;
    
        }else{
    
            //dont show footerview below table view if cells are outside screen bounds
            [footerHeightConstraint setConstant:0.0f];
        }
    
    }
    

    注意:我错过了一个与高度相关的边缘情况,例如行*高度的乘积等于或小于屏幕边界,所以你必须看看.

    【讨论】:

    • 好主意。但这只是意味着 UITablefooterview 不能做到这一点?
    • 据我所知,没有。您看到的任何行为都是默认行为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多