UITableView添加footerView按钮

本文介绍了如何在UITableView中添加自定义的footerView按钮,包括不设置尺寸的添加方式和通过代码创建的方式。在代码创建中,需要注意footerBtn的高度设置,以及可以使用nil代替[NSBundle mainBundle]获取xib文件,同时指出tableFooterView中的控件位置和宽度不可调,建议使用UIView的xib文件来描述底部布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


UITableView添加自定义的footerView按钮

注:不需要设置footer的尺寸

// 设置footerView
// 注意:不需要设置footer的尺寸
MJTgFooterView *footer = [MJTgFooterView footerView];
footer.controller = self;
self.tableView.tableFooterView = footer;

UITableView添加代码创建的footerView按钮

// 设置tableview尾部显示的控件(tableFooterView的宽度永远是tableView的宽度)
// tableFooterView只需要设置高度
UIButton *footerBtn = [UIButton buttonWithType:UIButtonTypeSystem];
footerBtn.frame = CGRectMake(0, 0, 0, 35);
footerBtn.backgroundColor = [UIColor orangeColor];
[footerBtn setTitle:@"加载更多团购" forState:UIControlStateNormal];

// 创建nib对象
// UINib *nib = [UINib nibWithNibName:@"MJTgFooterView" bundle:[NSBundle mainBundle]];

// 创建nib对象(也可以传入nil - 解释:凡是参数需要传入[NSBundle mainBundle]的都可以使用nil代替,因为默认的就是mainBundle)
UINib *nib = [UINib nibWithNibName:@"MJTgFooterView" bundle:nil];

// 因为在tableFooterView中的控件是不可调位置和宽度的,所以要新建个UIView的xib文件来描述底部控件
// 加载xib/nib文件
UIView *footerView = [[nib instantiateWithOwner:nil options:nil] lastObject];
self.tableView.tableFooterView = footerView;

以上代码的注意点:
1)footerBtn.frame = CGRectMake(0, 0, 0, 35);
//前三个属性不需要设置,只需要设置一个高度,宽度不需要去设置,宽度就是UIViewTable的宽度

2)使用另一种方法来获取xib文件,之前使用[NSBundle mainBundle]来获取的

UINib *nib = [UINib nibWithNibName:@"MJTgFooterView" bundle:[NSBundle mainBundle]];

// 之前使用[NSBundle mainBundle]来获取的
NSBundle *bundle = [NSBundle mainBundle];
NSArray *objs = [bundle loadNibNamed:@"MJTgFooterView" owner:nil options:nil];
UIView *appView = [objs lastObject];

注:也可以传入nil - 解释:凡是参数需要传入[NSBundle mainBundle]的都可以使用nil代替,因为默认的就是mainBundle

UINib *nib = [UINib nibWithNibName:@"MJTgFooterView" bundle:nil];

3)因为在tableFooterView中的控件是不可调位置和宽度的,所以要新建个UIView的xib文件来描述底部控件

UINib *nib = [UINib nibWithNibName:@"MJTgFooterView" bundle:nil];
UIView *footerView = [[nib instantiateWithOwner:nil options:nil] lastObject];

// 之前使用[NSBundle mainBundle]来获取的
NSBundle *bundle = [NSBundle mainBundle];
NSArray *objs = [bundle loadNibNamed:@"MJTgFooterView" owner:nil options:nil];
UIView *appView = [objs lastObject];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值