【发布时间】:2011-05-17 12:39:38
【问题描述】:
我有一张普通风格的桌子,只有一个部分。我有一个已实现的viewForHeaderInSection: 在部分标题中添加自定义视图。
我无法在我的表格部分标题视图和我的第一个视图之间看到分隔线 细胞。 [见附图]
我做错了什么?
【问题讨论】:
标签: ios uitableview cocoa-touch uikit
我有一张普通风格的桌子,只有一个部分。我有一个已实现的viewForHeaderInSection: 在部分标题中添加自定义视图。
我无法在我的表格部分标题视图和我的第一个视图之间看到分隔线 细胞。 [见附图]
我做错了什么?
【问题讨论】:
标签: ios uitableview cocoa-touch uikit
自定义页眉和页脚的下方/上方不包含分隔符。您需要自己在自定义视图中实现分隔符(或切换到分组样式,即使使用自定义页眉/页脚,也会在其上方和下方显示组的轮廓)。
【讨论】:
UITableView 初始化器之一允许您这样做
正如 Jeremy 在他的回答中指出的那样,iOS 不会在页眉/页脚上方/下方添加分隔符;您可以只使用 UIView 自己创建一条线。
以下是将标准外观的分隔符视图添加到标题视图的代码:
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;
【讨论】:
通过返回+1tableView: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](它不会显示,因此无需设置框架或任何东西)。您可能需要在您的UITableViewDataSource 和UITableViewDelegate 方法中进行一些-1 索引调整,但它可以工作(在iOS 7 中测试),并且 保证样式一致(无需自己绘制“假”分隔符——这是一个真正的系统绘制的 UITableView 分隔符)。
【讨论】:
在标题视图和第一行之间添加分隔符:- 在节委托方法中的标题视图中添加子视图 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);
}
}
【讨论】: