ios Header 和第一个单元格之间的分隔——在普通的 UITableView 中

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

Separation between Header and first cell -- In plain UITableView

iosuitableviewcocoa-touchuikit

提问by Abhinav

I have a table with with Plain style and only one section. I have a implemented viewForHeaderInSection:to add a custom view in the section header.

我有一张普通风格的桌子,只有一个部分。我实现viewForHeaderInSection:了在节标题中添加自定义视图。

I am not able to see a separation line between my table section header view and my first cell. [See the attached image]

我看不到表格部分标题视图和第一个单元格之间的分隔线。[见附图]

alt text

替代文字

What am I doing wrong?

我究竟做错了什么?

回答by JosephH

As Jeremy notes in his answer, iOS doesn't add separators above/below the headers/footers; you can just use a UIView to create a line yourself.

正如 Jeremy 在他的回答中指出的那样,iOS 不会在页眉/页脚的上方/下方添加分隔符;您可以使用 UIView 自己创建一条线。

Here's the code to add a standard looking separator view to a header view:

这是将标准外观分隔符视图添加到标题视图的代码:

CGRect sepFrame = CGRectMake(0, headerView.frame.size.height-1, 320, 1);
seperatorView = [[[UIView alloc] initWithFrame:sepFrame] autorelease];
seperatorView.backgroundColor = [UIColor colorWithWhite:224.0/255.0 alpha:1.0];
[headerView addSubview:seperatorView];

If you're trying to make it look like a normal table view cell, you probably need to add one at the top of the header view too.

如果你想让它看起来像一个普通的表格视图单元格,你可能还需要在标题视图的顶部添加一个。

回答by Jeremy Fuller

Custom headers and footers do not contain separators below/above them. You'll need to implement the separator yourself in the custom view (or switch to grouped style, which will show the outline of the group above and below it even with a custom header/footer).

自定义页眉和页脚的下方/上方不包含分隔符。您需要在自定义视图中自己实现分隔符(或切换到分组样式,即使使用自定义页眉/页脚,它也会显示其上方和下方的组的轮廓)。

回答by Usman Nisar

if you want to give space only between table header and table first row then you can use

如果您只想在表格标题和表格第一行之间留出空间,那么您可以使用

In method of tableView:heightForHeaderInSection:(NSInteger)section

在方法中 tableView:heightForHeaderInSection:(NSInteger)section

if(section ==0)
    return 3; // (space u want to give between header and first row);

return 10; //(ur section header height)

In method of tableView:viewForHeaderInSection:(NSInteger)section

在方法中 tableView:viewForHeaderInSection:(NSInteger)section

  UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 3)];
  headerView.backgroundColor = [UIColor clearColor];   // use your own design

  return headerView;

回答by devios1

Add an extra "hidden" row to the section you want to add the separator to by returning +1the existing number of rows in tableView:numberOfRowsInSection:. Then add the following method:

添加一个额外的“隐藏”行,你想通过返回到分离器添加到部分+1现有的行数tableView:numberOfRowsInSection:。然后添加以下方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ( indexPath.section == sectionOfHiddenRow && indexPath.row == indexOfHiddenRow )
        return 0.f;
    else
        return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}

If you want the separator at the top of a section (after a header), indexOfHiddenRowwill be 0. If you want it at the bottom of a section (before a footer) it will be [self tableView:tableView numberOfRowsInSection:sectionOfHiddenRow] - 1.

如果你想在一个部分的顶部(在标题之后)分隔符,indexOfHiddenRow将是0. 如果您希望它位于部分的底部(在页脚之前),它将是[self tableView:tableView numberOfRowsInSection:sectionOfHiddenRow] - 1.

Now inside tableView:cellForRowAtIndexPath:, simply return [UITableViewCell new]for the hidden row (it won't be displayed, so no need to set a frame or anything). You may need to do some -1index adjustments in your UITableViewDataSourceand UITableViewDelegatemethods, but it works (tested in iOS 7), andguarantees consistent styling (no need to draw your own "fake" separator—this is a true system-drawn UITableViewseparator).

现在在里面tableView:cellForRowAtIndexPath:,只需返回[UITableViewCell new]隐藏的行(它不会显示,所以不需要设置框架或任何东西)。您可能需要-1在您的UITableViewDataSourceUITableViewDelegate方法中进行一些索引调整,但它有效(在 iOS 7 中测试),保证一致的样式(无需绘制自己的“假”分隔符——这是一个真正的系统绘制的UITableView分隔符)。

回答by Jorg B Jorge

I've extended UITableViewCell with a couple of separator methods (in Swift). With them I can add separators to headers or remove them from regular cells. I hope it may help some people.

我用几个分隔符方法(在 Swift 中)扩展了 UITableViewCell。有了它们,我可以在标题中添加分隔符或从常规单元格中删除它们。我希望它可以帮助一些人。

public extension UITableViewCell
{
    func addSeparator(y: CGFloat, margin: CGFloat, color: UIColor)
    {
        let sepFrame = CGRectMake(margin, y, self.frame.width - margin, 0.7);
        let seperatorView = UIView(frame: sepFrame);
        seperatorView.backgroundColor = color;
        self.addSubview(seperatorView);
    }

    public func addTopSeparator(tableView: UITableView)
    {
        let margin = tableView.separatorInset.left;

        self.addSeparator(0, margin: margin, color: tableView.separatorColor!);
    }

    public func addBottomSeparator(tableView: UITableView, cellHeight: CGFloat)
    {
        let margin = tableView.separatorInset.left;

        self.addSeparator(cellHeight-2, margin: margin, color: tableView.separatorColor!);
    }

    public func removeSeparator(width: CGFloat)
    {
        self.separatorInset = UIEdgeInsetsMake(0.0, width, 0.0, 0.0);
    }

}

回答by YaBoiSandeep

Add a separator between header view and first row :- In view for Header in section delegate method add a subview self.separator //@property (nonatomic, strong) UIImageView *separator;

在标题视图和第一行之间添加一个分隔符:- 在部分委托方法中的标题视图中添加一个子视图 self.separator //@property (nonatomic, strong) UIImageView *separator;

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

return 41; 
}


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

self.headerView = [[UIView alloc] init];
self.headerView.backgroundColor = [UIUtils colorForRGBColor:TIMESHEET_HEADERVIEW_COLOR];

self.separator = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"seperator.png"]];
self.separator.frame = CGRectMake(0,40,self.view.frame.size.width,1);
[self.headerView addSubview:self.separator];
return self.headerView;

}