如何在UITableView的顶部添加额外的分隔符?

ric*_*cht 34 iphone cocoa-touch objective-c uikit ios

我有一个基本上分为两部分的iPhone视图,上半部分有一个信息显示,下半部分有一个用于选择动作的UITableView.问题是UITableView中第一个单元格上方没有边框或分隔符,因此列表中的第一个项目看起来很有趣.如何在表格顶部添加额外的分隔符,将其与上方的显示区域分开?

这是构建单元格的代码 - 它非常简单.整体布局在xib中处理.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    switch(indexPath.row) {
        case 0: {
            cell.textLabel.text = @"Action 1";
            break;
        }
        case 1: {
            cell.textLabel.text = @"Action 2";
            break;
        }
        // etc.......
    }
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

Ort*_*ntz 61

要复制标准的iOS分隔线,我使用1 px(不是1 pt)发条线tableHeaderView和表格视图separatorColor:

// in -viewDidLoad
self.tableView.tableHeaderView = ({UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1 / UIScreen.mainScreen.scale)];
    line.backgroundColor = self.tableView.separatorColor;
    line;
});
Run Code Online (Sandbox Code Playgroud)

斯威夫特也是如此(感谢Dane Jordan,Yuichi Kato,Tony Merritt):

let px = 1 / UIScreen.main.scale
let frame = CGRect(x: 0, y: 0, width: self.tableView.frame.size.width, height: px)
let line = UIView(frame: frame)
self.tableView.tableHeaderView = line
line.backgroundColor = self.tableView.separatorColor
Run Code Online (Sandbox Code Playgroud)

  • 这称为语句表达式(http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html).当然你也可以在没有它的情况下编写它,但它很好地定义了UIView的定义. (4认同)
  • 它是用({object})调用的; 符号? (3认同)
  • 对于快速用户:`var px = 1/UIScreen.mainScreen().scale var frame = CGRectMake(0,0,self.tableView.frame.size.width,px)var line:UIView = UIView(frame:frame)self .tableView.tableHeaderView = line line.backgroundColor = self.tableView.separatorColor` (2认同)
  • 对于Swift 3.0用户,让px = 1/UIScreen.main.scale让frame = CGRect(x:0,y:0,width:self.tableView.frame.size.width,height:px)let line:UIView = UIView( frame:frame)self.tableView.tableHeaderView = line line.backgroundColor = self.tableView.separatorColor (2认同)

Jas*_*son 14

我刚刚遇到同样的问题,并意识到顶部的分隔符只在滚动表格时显示.

然后我做了以下几点

  1. 在Interface Builder中,转到"滚动视图大小"
  2. 将Top的Content Insets设置为1

或者在代码中你可以做

[tableView setContentInset:UIEdgeInsetsMake(1.0, 0.0, 0.0, 0.0)];
Run Code Online (Sandbox Code Playgroud)

注意:这不再适用于iOS7,因为分隔符根本不再显示.


ddi*_*ego 11

我有同样的问题,无法找到答案.所以我在表格标题的底部添加了一行.

CGRect  tableFrame = [[self view] bounds] ; 
CGFloat headerHeight = 100;        
UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableFrame.size.width, headerHeight)];
// Add stuff to my table header...

// Create separator
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, headerHeight-1, tableFrame.size.width, 1)] ;
lineView.backgroundColor = [UIColor colorWithRed:224/255.0 green:224/255.0 blue:224/255.0 alpha:1.0];
[headerView addSubview:lineView];

self.tableView.tableHeaderView = headerView;
Run Code Online (Sandbox Code Playgroud)


fl0*_*034 5

我做了一个UITableView扩展,它在滚动表时在UITableView的顶部显示了本机样式分隔符。

这是它的样子

这是代码(Swift 3)

fileprivate var _topSeparatorTag = 5432 // choose unused tag

extension UITableView {

    fileprivate var _topSeparator: UIView? {
        return superview?.subviews.filter { $0.tag == _topSeparatorTag }.first
    }

    override open var contentOffset: CGPoint {
        didSet {
            guard let topSeparator = _topSeparator else { return }

            let shouldDisplaySeparator = contentOffset.y > 0

            if shouldDisplaySeparator && topSeparator.alpha == 0 {
                UIView.animate(withDuration: 0.15, animations: {
                    topSeparator.alpha = 1
                })
            } else if !shouldDisplaySeparator && topSeparator.alpha == 1 {
                UIView.animate(withDuration: 0.25, animations: {
                    topSeparator.alpha = 0
                })
            }
        }
    }

    // Adds a separator to the superview at the top of the table
    // This needs the separator insets to be set on the tableView, not the cell itself
    func showTopSeparatorWhenScrolled(_ enabled: Bool) {
        if enabled {
            if _topSeparator == nil {
                let topSeparator = UIView()
                topSeparator.backgroundColor = separatorColor?.withAlpha(newAlpha: 0.85) // because while scrolling, the other separators seem lighter
                topSeparator.translatesAutoresizingMaskIntoConstraints = false

                superview?.addSubview(topSeparator)

                topSeparator.leftAnchor.constraint(equalTo: self.leftAnchor, constant: separatorInset.left).isActive = true
                topSeparator.rightAnchor.constraint(equalTo: self.rightAnchor, constant: separatorInset.right).isActive = true
                topSeparator.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
                let onePixelInPoints = CGFloat(1) / UIScreen.main.scale 
                topSeparator.heightAnchor.constraint(equalToConstant: onePixelInPoints).isActive = true

                topSeparator.tag = _topSeparatorTag
                topSeparator.alpha = 0

                superview?.setNeedsLayout()
            }
        } else {
            _topSeparator?.removeFromSuperview()
        }
    }

    func removeSeparatorsOfEmptyCells() {
        tableFooterView = UIView(frame: .zero)
    }
}
Run Code Online (Sandbox Code Playgroud)

要启用它,只需tableView.showTopSeparatorWhenScrolled(true)delegateUITableView


Vad*_*aev 5

斯威夫特4

extension UITableView {
    func addTableHeaderViewLine() {
        self.tableHeaderView = {
            let line = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: 1 / UIScreen.main.scale))
            line.backgroundColor = self.separatorColor
            return line
        }()
    }
}
Run Code Online (Sandbox Code Playgroud)