标题和第一个单元格之间的分隔 – 在普通的UITableView

我有一个平原与只有一个部分的表。 我有一个实现viewForHeaderInSection:在部分标题中添加自定义视图。

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

替代文字

我究竟做错了什么?

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

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

以下是将标准查看分隔视图添加到标题视图的代码:

 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]; 

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

如果你只想给表头和表第一行之间的空间,那么你可以使用

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) 

tableView:viewForHeaderInSection:(NSInteger)section

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

通过返回+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]; } 

如果你想在一个部分的顶部(头部之后)的分隔符, indexOfHiddenRow将是0 。 如果你想在一个部分的底部(在页脚之前)它将是[self tableView:tableView numberOfRowsInSection:sectionOfHiddenRow] - 1

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

在标题视图和第一行之间添加一个分隔符: – 在部分委托方法的Header中添加一个子视图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; } 

我用一些分隔符方法(在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); } }