ios 自定义 UITableView 标题部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15611374/
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-30 22:51:57  来源:igfitidea点击:

Customize UITableView header section

iosobjective-cswiftuitableview

提问by limon

I want to customize UITableViewheader for each section. So far, I've implemented

我想UITableView为每个部分自定义标题。到目前为止,我已经实施了

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

this UITabelViewDelegatemethod. What I want to do is to get current header for each section and just add UILabelas a subview.

这种UITabelViewDelegate方法。我想要做的是获取每个部分的当前标题,然后添加UILabel为子视图。

So far, I'm not able to accomplish that. Because, I couldn't find anything to get default section header. First question,is there any way to get default section header?

到目前为止,我无法做到这一点。因为,我找不到任何东西来获取默认部分标题。第一个问题,有没有办法获得默认部分标题

If it's not possible, I need to create a container view which is a UIViewbut,this time I need to set default background color,shadow color etc. Because, if you look carefully into section's header, it's already customized.

如果不可能,我需要创建一个容器视图,UIView但是,这次我需要设置默认背景颜色、阴影颜色等。因为,如果您仔细查看部分的标题,它已经是自定义的。

How can I get these default values for each section header ?

如何为每个部分标题获取这些默认值?

Thank you all.

谢谢你们。

回答by Lochana Ragupathy

You can try this:

你可以试试这个:

 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
    /* Create custom view to display section header... */
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
    [label setFont:[UIFont boldSystemFontOfSize:12]];
     NSString *string =[list objectAtIndex:section];
    /* Section header is in 0th index... */
    [label setText:string];
    [view addSubview:label];
    [view setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
    return view;
}

回答by samwize

The selected answer using tableView :viewForHeaderInSection:is correct.

使用的所选答案tableView :viewForHeaderInSection:是正确的。

Just to share a tip here.

只是在这里分享一个提示。

If you are using storyboard/xib, then you could create another prototype cell and use it for your "section cell". The code to configure the header is similar to how you configure for row cells.

如果您使用的是故事板/xib,那么您可以创建另一个原型单元格并将其用于您的“部分单元格”。配置标题的代码与您为行单元格配置的方式类似。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    static NSString *HeaderCellIdentifier = @"Header";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HeaderCellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:HeaderCellIdentifier];
    }

    // Configure the cell title etc
    [self configureHeaderCell:cell inSection:section];

    return cell;
}

回答by estemendoza

Swift version of Lochana Tejasanswer:

Lochana Tejas 的Swift 版本回答:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 18))
    let label = UILabel(frame: CGRectMake(10, 5, tableView.frame.size.width, 18))
    label.font = UIFont.systemFontOfSize(14)
    label.text = list.objectAtIndex(indexPath.row) as! String
    view.addSubview(label)
    view.backgroundColor = UIColor.grayColor() // Set your background color

    return view
}

回答by Mert

If you use default header view you can only change the text on it with

如果您使用默认标题视图,则只能更改其上的文本

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

For Swift:

对于斯威夫特:

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

If you want to customize the view you need to create a new one your self.

如果您想自定义视图,您需要自己创建一个新视图。

回答by user836773

回答by Kathen

If headerInSection isn't show, can try this.

如果 headerInSection 没有显示,可以试试这个。

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

This returns a height for the header of a given section.

这将返回给定部分的标题的高度。

回答by Adam

Swift 3 version of lochana and estemendoza answers:

Swift 3 版本的 lochana 和 estemendoza 答案:

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

    let view = UIView(frame: CGRect(x:0, y:0, width:tableView.frame.size.width, height:18))
    let label = UILabel(frame: CGRect(x:10, y:5, width:tableView.frame.size.width, height:18))
    label.font = UIFont.systemFont(ofSize: 14)
    label.text = "This is a test";
    view.addSubview(label);
    view.backgroundColor = UIColor.gray;
    return view

}

Also, be advised that you ALSO have to implement:

另外,请注意,您还必须实施:

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

回答by Craig Brown

The other answers do a good job of recreating the default header view, but don't actually answer your main question:

其他答案在重新创建默认标题视图方面做得很好,但实际上并没有回答您的主要问题:

is there any way to get default section header ?

有没有办法获得默认部分标题?

There is a way - just implement tableView:willDisplayHeaderView:forSection:in your delegate. The default header view will be passed into the second parameter, and from there you can cast it to a UITableViewHeaderFooterViewand then add/change subviews as you wish.

有一种方法 - 只需tableView:willDisplayHeaderView:forSection:在您的委托中实施。默认标题视图将传递到第二个参数中,您可以从那里将其转换为 a UITableViewHeaderFooterView,然后根据需要添加/更改子视图。

Obj-C

对象-C

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;

    // Do whatever with the header view... e.g.
    // headerView.textLabel.textColor = [UIColor whiteColor]
}

Swift

迅速

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
    let headerView = view as! UITableViewHeaderFooterView

    // Do whatever with the header view... e.g.
    // headerView.textLabel?.textColor = UIColor.white
}

回答by Gigi

Try this......

尝试这个......

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) 
{
    // Background view is at index 0, content view at index 1
    if let bgView = view.subviews[0] as? UIView
    {
        // do your stuff
    }

    view.layer.borderColor = UIColor.magentaColor().CGColor
    view.layer.borderWidth = 1
}

回答by Anish Kumar

This is the easiest solution possible. The following code can be used directly for creating a custom section header.

这是最简单的解决方案。以下代码可直接用于创建自定义节标题。

 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    SectionHeaderTableViewCell *headerView = [tableView dequeueReusableCellWithIdentifier:@"sectionHeader"];

    //For creating a drop menu of rows from the section
    //==THIS IS JUST AN EXAMPLE. YOU CAN REMOVE THIS IF-ELSE.==
    if (![self.sectionCollapsedArray[section] boolValue])
    {
        headerView.imageView.image = [UIImage imageNamed:@"up_icon"];
    }
    else
    {
        headerView.imageView.image = [UIImage imageNamed:@"drop_icon"];
    }

    //For button action inside the custom cell
    headerView.dropButton.tag = section;
    [headerView.dropButton addTarget:self action:@selector(sectionTapped:) forControlEvents:UIControlEventTouchUpInside];

    //For removing long touch gestures.
    for (UIGestureRecognizer *recognizer in headerView.contentView.gestureRecognizers)
    {
        [headerView.contentView removeGestureRecognizer:recognizer];
        [headerView removeGestureRecognizer:recognizer];
    }

    return headerView.contentView;
}

NOTE: SectionHeaderTableViewCell is a custom UITableViewCell created in Storyboard.

注意: SectionHeaderTableViewCell 是在 Storyboard 中创建的自定义 UITableViewCell。