ios 以编程方式向 UITableview 添加标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19813412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:47:04  来源:igfitidea点击:

Adding a header to UITableview programmatically

ios

提问by Allreadyhome

How would I add this feature programmatically? I'm not sure what I would search to find this. I know how to add the IBAction but not the code to generate this feature highlighted in the picture. Is it a custom cell or a divider?s

我将如何以编程方式添加此功能?我不确定我会搜索什么来找到这个。我知道如何添加 IBAction 但不知道生成图片中突出显示的此功能的代码。它是自定义单元格还是分隔符?

http://i40.tinypic.com/hthhdl.png

http://i40.tinypic.com/hthhdl.png

回答by Antonio MG

That's just a header in the TableView, they appear on top of your section

这只是 TableView 中的一个标题,它们出现在您的部分的顶部

Use this method if you want a title:

如果您想要标题,请使用此方法:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

Or this one if you want a custom view:

如果您想要自定义视图,或者这个:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

And for the height:

对于高度:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

回答by Priyank Jotangiya

Try This one Programmatically adding headerit's Easy..

试试这个以编程方式添加标题很容易..

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(1, 50, 276, 30)];
headerView.backgroundColor = [UIColor colorWithRed:235/255.0f green:235/255.0f blue:235/255.0f alpha:1.0f];

UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(4, 5, 276, 24)];
labelView.text = @"hello";

[headerView addSubview:labelView];
self.tableView.tableHeaderView = headerView;

Or Try in Swift

或者试试 Swift

var headerView: UIView = UIView.init(frame: CGRectMake(1, 50, 276, 30))
headerView.backgroundColor = UIColor(red: 235/255.0, green: 235/255.0, blue: 235/255.0, alpha: 1.0)

var labelView: UILabel = UILabel.init(frame: CGRectMake(4, 5, 276, 24))
labelView.text = "hello"

headerView.addSubview(labelView)
self.tableView.tableHeaderView = headerView

Swift 4.0 and later

Swift 4.0 及更高版本

var headerView: UIView = UIView.init(frame: CGRect.init(x: 1, y: 50, width: 276, height: 30))
headerView.backgroundColor = UIColor(red: 235/255.0, green: 235/255.0, blue: 235/255.0, alpha: 1.0)

var labelView: UILabel = UILabel.init(frame: CGRect.init(x: 4, y: 5, width: 276, height: 24))
labelView.text = "hello"

headerView.addSubview(labelView)
self.tableView.tableHeaderView = headerView

回答by fred

Updated For Swift 4

为 Swift 4 更新

Similar to SimplePJ's answer. If you want to create a custom header view for the ENTIRE TABLE VIEWyou can create one and set it as the tableHeaderView like so.

类似于 SimplePJ 的回答。如果您想为整个表视图创建自定义标题视图,您可以创建一个并将其设置为 tableHeaderView 像这样。

  let headerView: UIView = UIView.init(frame: CGRect(x: 1, y: 50, width: 276, height: 30))
  headerView.backgroundColor = .red

  let labelView: UILabel = UILabel.init(frame: CGRect(x: 4, y: 5, width: 276, height: 24))
  labelView.text = "My header view"

  headerView.addSubview(labelView)
  self.tableView.tableHeaderView = headerView

This is not to be confused with creating a table view section headerin which you'd use the following method.

不要将这与创建表视图节标题混淆,您将在其中使用以下方法。

func headerView(forSection section: Int) -> UITableViewHeaderFooterView?

The return value is the header view associated with the section, or nil if the section does not have a header view.

返回值是与section关联的标题视图,如果该部分没有标题视图,则返回nil。

回答by l0gg3r

Just implement these two delegate methods in your TableViewController

只需在您的 TableViewController 中实现这两个委托方法

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

In the first method return the height of the header view, In the second return the view that should be displayed

在第一个方法中返回标题视图的高度,在第二个方法中返回应该显示的视图

here is an example

这是一个例子

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 55.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"simpleHeader.png"]];
}