我想在每个单元格之间增加10像素的间距。我该怎么做?
而我已经添加了这段代码
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
我想在每个单元格之间增加10像素的间距。我该怎么做?
而我已经添加了这段代码
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
对我来说,最好的方法就是将这段代码添加到cellForRowAtIndexPath或willDisplayCell中。
CGRect sizeRect = [UIScreen mainScreen].applicationFrame;
NSInteger separatorHeight = 3;
UIView * additionalSeparator = [[UIView alloc] initWithFrame:CGRectMake(0,cell.frame.size.height-separatorHeight,sizeRect.size.width,separatorHeight)];
additionalSeparator.backgroundColor = [UIColor grayColor];
[cell addSubview:additionalSeparator];
对于 Swift 3.0 版本:
let screenSize = UIScreen.main.bounds
let separatorHeight = CGFloat(3.0)
let additionalSeparator = UIView.init(frame: CGRect(x: 0, y: self.frame.size.height-separatorHeight, width: screenSize.width, height: separatorHeight))
additionalSeparator.backgroundColor = UIColor.gray
self.addSubview(additionalSeparator)
你应该将这个代码添加到单元格的awakeFromNib()方法中,以避免重新创建。
additionalSeparator
,以免重复添加。由于单元格重用的原因,cellForRow...
和willDisplayCell
可能会被多次调用,这会导致用户在滚动表格时性能下降。请注意优化。 - ikuramedia我见过很多笨拙的解决方案,比如用隐藏的单元格对UITableView
进行子类化,以及其他不太理想的解决方法(本主题中也包含)。
在初始化UITableView
时,将UITableView
的rowHeight
属性设置为一个高度,该高度等于单元格的高度 + 所需分隔符/空白区域的高度。
但不要使用标准的UITableViewCell
类,而是对UITableViewCell
类进行子类化,并重写其layoutSubviews
方法。在那里,在调用super
之后(不要忘记),将单元格本身的高度设置为所需高度。
BONUS UPDATE 18/OCT/2015:
你可以聪明一点。上面的解决方法基本上将“分隔符”放在了单元格底部。实际上,行高由TableViewController管理,但单元格被调整为更低的高度。这导致分隔符/空白间隔在底部。但是,你还可以垂直居中所有子视图,以便在顶部和底部留下相同的空间。例如1pt和1pt。
你还可以在单元格子类中创建isFirst、isLast的便利属性。在cellForRowAtIndexPath中将其设置为yes。这样,你就可以在layoutSubviews方法中处理顶部和底部分隔线的边缘情况,因为该方法可以访问到这些属性。 这样你可以处理顶部或底部的特殊情况 - 因为有时设计部门想要N+1个分隔符,而单元格的数量只有N个。因此,你必须以一种特殊的方式处理顶部或底部的分隔线。但最好是在单元格内部处理这些情况,而不是在tableViewHeader或TableViewFooter中处理。
我认为使用标准 API 是不可能的。我猜你需要子类化 UITableViewCell 并添加一个视图,模拟一个在单元格底部的分隔符。
你可能想要查看这个问题,它看起来相关并且有一些示例代码: iPhone + UITableView + place an image for separator
cellForRowAtIndexPath
或 willDisplayCell
中:override func tableView(tableView: UITableView,
willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath)
{
let additionalSeparatorThickness = CGFloat(3)
let additionalSeparator = UIView(frame: CGRectMake(0,
cell.frame.size.height - additionalSeparatorThickness,
cell.frame.size.width,
additionalSeparatorThickness))
additionalSeparator.backgroundColor = UIColor.redColor()
cell.addSubview(additionalSeparator)
}
只需稍微增加单元格的高度,并为单元格分配一个遮罩层,就像这样:
```func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "...", for: indexPath)
// Configure the cell...
let maskLayer = CAShapeLayer()
let bounds = cell.bounds
maskLayer.path = UIBezierPath(roundedRect: CGRect(x: 2, y: 2, width: bounds.width-4, height: bounds.height-4), cornerRadius: 5).cgPath
cell.layer.mask = maskLayer
return cell
}
虽然这是一个有点老的帖子,但是在这个帖子中我只找到了一些hacky(指不规范的)解决方案,下面是对我最有效的解决方案的介绍(不需要在每个单元格中添加额外的UIView)。
Swift 3:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//configure your cell...
cell.layer.shadowColor = UIColor.red.cgColor
cell.layer.shadowOffset = CGSize(width: 0, height: 1)
cell.layer.shadowOpacity = 1
cell.layer.shadowRadius = 0
cell.layer.masksToBounds = false
return cell
}
编辑:不幸的是,如果您在表格中向上滚动,这种方法就不起作用了。无论如何,我仍然在此处提供答案,因为它可能是您的表格有限内容的解决方案。
更多信息请查看“滚动时UITableViewCell上的阴影消失”
默认分隔符的高度无法更改。相反,您需要为每个单元格添加一个看起来像分隔符的子视图(并可选地使单元格更高)。例如,您可以在cellForRowAtIndexPath
或UITableViewCell
子类中执行此操作。
如果允许选择单元格,则还需要添加所选状态的子视图,否则当选择单元格时分隔符将消失。这就是为什么还要配置selectedBackgroundView
的原因。
将以下内容添加到您的UITableViewController
子类中:
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorStyle = .none
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.backgroundView = UIView(backgroundColor: .white)
cell.backgroundView?.addSeparator()
cell.selectedBackgroundView = UIView(backgroundColor: .blue)
cell.selectedBackgroundView?.addSeparator()
// configure the cell
return cell
}
请将以下扩展程序添加到同一文件的底部:
private extension UIView {
convenience init(backgroundColor: UIColor) {
self.init()
self.backgroundColor = backgroundColor
}
func addSeparator() {
let separatorHeight: CGFloat = 2
let frame = CGRect(x: 0, y: bounds.height - separatorHeight, width: bounds.width, height: separatorHeight)
let separator = UIView(frame: frame)
separator.backgroundColor = .gray
separator.autoresizingMask = [.flexibleTopMargin, .flexibleWidth]
addSubview(separator)
}
}
对于高度为50且行间距为5像素的表格单元格。宽度为320。
将单元格的背景定义为透明:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor clearColor];
}
设置单元格的高度,即行的大小加上分隔符的大小:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 55;
}
UILabel *headerBackgroundLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,320,50)];
backgroundBox.backgroundColor = [UIColor grayColor];
[cell addSubview:backgroundBox];
我以更简单和灵活的方式完成。有些人可能称之为黑客行为,但我认为它很实用。
我隐藏了标准的UITableViewSeparator。然后,在单元格中添加一个子视图,使用自动布局将其固定在顶部。将其高度设为所需高度。两侧留有一定的边距并将其钉住到边缘。改变它的背景颜色。这样我就拥有了我想要的纯分隔符。
你可能会质疑在单元格层次结构中再加入另一个UIView的效率。它真的会有明显的区别吗?也许不会——毕竟,你已经将标准分隔符从表格层次结构中删除了。