反正有没有用一个通用页脚创建UITableView? [英] Is there anyway to create a UITableView with one universal footer?

查看:35
本文介绍了反正有没有用一个通用页脚创建UITableView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建具有多个部分的 UITableView .每个部分都有其自己的页眉,但我试图为整个表格视图创建一个通用页脚,并将其保持在一个位置...

使用 UITableViewDelegate 方法是否可以实现这种逻辑?还是应该创建一个自定义视图,然后尝试将其作为子视图添加到我的表视图中?我当前拥有的页脚包含一个 UIButton .

如果有人有一些很好的示例代码.

此问题与所引用的问题不同.我正在尝试使通用页脚浮在 UITableView 上方.另一个问题没有指定页脚的位置,只是需要一个页脚.

解决方案

我最终创建了一个子视图并将按钮添加到该视图中.然后,我将该视图设为页脚".

这是给我想要的结果的代码.

 -(void)viewDidLoad{[super viewDidLoad];//self.tableView.delegate = self;//self.tableView.dataSource = self;//保存当前的tableview,然后用常规的uiview替换viewself.tableView =(UITableView *)self.view;UIView * replacementView = [[UIView分配] initWithFrame:self.tableView.frame];self.view =替换视图;[self.view addSubview:self.tableView];UIView * footerView = [[UIView alloc] initWithFrame:CGRectMake(0,370,320,45)];//创建按钮UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];//button.userInteractionEnabled = YES;//按钮应与表格视图单元格一样大//可以设置按钮的宽度,但是添加到该按钮的视图的宽度将始终与tableView的宽度匹配[button setFrame:CGRectMake(60,0,200,45)];//设置标题,字体大小和字体颜色[按钮setTitle:@"Build" forState:UIControlStateNormal];[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];[按钮setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//设置按钮的动作[按钮addTarget:self动作:@selector(buildThenSegue)forControlEvents:UIControlEventTouchUpInside];//将按钮添加到视图[footerView addSubview:button];footerView.userInteractionEnabled = YES;[self.view addSubview:footerView];self.tableView.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height); 

}

请注意,这在 UITableViewController 的子类中.

我将此答案引用了另一个问题: https://stackoverflow.com/a/9084267/1091868

I'm trying to create a UITableView with multiple sections. Each section has it's own header, but I am trying to make a universal footer for the entire table view that stays in one position...

Is this logic possible using the UITableViewDelegate methods? Or should I create a custom view and just try and add it as a subview to my table view? The footer I currently have contains a UIButton.

If anyone has some sample code that would be great.

Edit: This question is not the same as the one referenced. I am trying to make a universal footer that floats above the UITableView. The other question does not specify the location of the footer, only that a footer is desired.

解决方案

I ended up creating a subview and adding my button to that view. I then made that view my "footer".

Here is the code that gave me the desired results.

- (void)viewDidLoad
{

[super viewDidLoad];

//self.tableView.delegate = self;
//self.tableView.dataSource = self;

//save current tableview, then replace view with a regular uiview
self.tableView = (UITableView*)self.view;
UIView *replacementView = [[UIView alloc] initWithFrame:self.tableView.frame];
self.view = replacementView;
[self.view addSubview:self.tableView];

UIView *footerView  = [[UIView alloc] initWithFrame:CGRectMake(0, 370, 320, 45)];

//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//button.userInteractionEnabled = YES;

//the button should be as big as a table view cell
//width of the button can be set but the width of the view it is added to will always match the width of the tableView
[button setFrame:CGRectMake(60, 0, 200, 45)]; 

//set title, font size and font color
[button setTitle:@"Build" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];   
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

//set action of the button
[button addTarget:self action:@selector(buildThenSegue)
 forControlEvents:UIControlEventTouchUpInside];

//add the button to the view
[footerView addSubview:button];
footerView.userInteractionEnabled = YES;

[self.view addSubview:footerView];
self.tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

}

Take note that this is in a subclass of UITableViewController.

I referenced this answer to another question: https://stackoverflow.com/a/9084267/1091868

这篇关于反正有没有用一个通用页脚创建UITableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆