vin*_*nay 48 iphone uitableview
我希望每个之间有更多的空间(10px)cell.我怎样才能做到这一点?
我添加了这段代码
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
Run Code Online (Sandbox Code Playgroud)
Dmi*_*iov 25
对我来说最好的方法,只需在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];
Run Code Online (Sandbox Code Playgroud)
对于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)
Run Code Online (Sandbox Code Playgroud)
您应该将其添加到单元格的方法awakeFromNib()以避免重新创建.
Ear*_*rey 22
我见过许多笨重的解决方案,比如UITableView使用隐藏单元格进行子类化,以及其他不太理想的解决方案.在这个帖子里.
初始化时UITableView,将rowHeight属性设置为UITableView等于=单元格高度+所需分隔符/空间高度的高度.
UITableViewCell但是,不要使用标准类,而是继承UITableViewCell类并重写其layoutSubviews方法.在调用之后super(不要忘记),将单元格本身的高度设置为所需的高度.
奖金更新18/OCT/2015:
你可以对此有点聪明.上面的解决方案基本上将"分隔符"放在单元格的底部.真正发生的是,行高由TableViewController管理,但是单元格的大小调整得有点低.这导致分隔符/空白空间位于底部.但您也可以垂直居中所有子视图,以便在顶部和底部留出相同的空间.例如1pt和1pt.
您还可以在单元子类上创建isFirst,isLast便捷属性.您可以在cellForRowAtIndexPath中将它们设置为yes.这样,您可以处理layoutSubviews方法中顶部和底部分隔符的边缘情况,因为这可以访问这些属性.通过这种方式,您可以处理顶部或底部的边缘情况 - 因为有时设计部门需要N + 1个分隔符,而单元格数量仅为N.因此您必须以特殊方式处理顶部或引导部分.但最好在单元格中执行此操作,而不是tableViewHeader或TableViewFooter.
May*_*jak 12
我不认为使用标准API是可能的.我想你需要继承UITableViewCell并添加一个模拟单元格底部分隔符的视图.
您可能想要检查这个问题,它似乎相关并且有一些示例代码: iPhone + UITableView +为分隔符放置图像
在斯威夫特
对我来说,最简单和最简单的办法是添加下面的代码片段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)
}
Run Code Online (Sandbox Code Playgroud)
您可以在故事板中完全执行此操作.方法如下:
就这样.你的分隔符将是20个单位高.
这是相当古老的。不过我会发布我的方法。
只需稍微增加单元格高度并为单元格分配一个遮罩层,如下所示:
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
}
Run Code Online (Sandbox Code Playgroud)
所以在这个例子中,我的分隔符高度为 4。
玩得开心!
| 归档时间: |
|
| 查看次数: |
76894 次 |
| 最近记录: |