我有一个 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;
}
为了复制标准的 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;
});
Swift 也是如此(感谢 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)