我希望每个cell
之间有更多空间(10px)。我怎么能这样做?
我已添加此代码
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
答案 0 :(得分:24)
对我来说最好的方法,只需在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()中以避免重新创建。
答案 1 :(得分:22)
我见过许多笨重的解决方案,比如将UITableView
子类化为隐藏的单元格,以及其他不太理想的解决方案。在这个帖子里。
初始化UITableView
时,将rowHeight
的{{1}}属性设置为等于=单元格高度+所需分隔符/空间高度的高度。
不要使用标准UITableView
类,而是继承UITableViewCell
类并覆盖其UITableViewCell
方法。在调用layoutSubviews
后(不要忘记),将单元格的高度设置为所需的高度。
奖励更新18 / OCT / 2015:
你可以对此有点聪明。上面的解决方案基本上将“分隔符”放在单元格的底部。真正发生的是,行高由TableViewController管理,但是单元格的大小调整得有点低。这导致分隔符/空白空间位于底部。但您也可以垂直居中所有子视图,以便在顶部和底部留出相同的空间。例如1pt和1pt。
您还可以在细胞子类上创建isFirst,isLast便利属性。您可以在cellForRowAtIndexPath中将它们设置为yes。 这样,您可以处理layoutSubviews方法中顶部和底部分隔符的边缘情况,因为这可以访问这些属性。 通过这种方式,您可以处理顶部或底部的边缘情况 - 因为有时设计部门需要N + 1个分隔符,而单元格数量仅为N.因此您必须以特殊方式处理顶部或引导部分。但最好在单元格内执行此操作,而不是tableViewHeader或TableViewFooter。
答案 2 :(得分:10)
我不认为使用标准API是可能的。我想你需要继承UITableViewCell并添加一个模拟单元格底部分隔符的视图。
您可能想查看此问题,它似乎相关并且有一些示例代码: iPhone + UITableView + place an image for separator
答案 3 :(得分:7)
在 Swift
中对我来说,最简单,最简单的方法是在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)
}
答案 4 :(得分:6)
您可以在故事板中完全执行此操作。方法如下:
这就是全部。你的分隔符将高达20个单位。
答案 5 :(得分:4)
只需稍微增加单元格高度并为单元格指定一个遮罩层,如下所示:
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
}
所以在这个例子中,我的分隔符高度为4。
玩得开心!
答案 6 :(得分:3)
有点老线程,但因为我在这个帖子中只找到了hacky解决方案, 这里最适合我的解决方案(在每个单元格中没有额外的UIView)
斯威夫特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
}
编辑:不幸的是,如果您在表格中向上滚动,这不起作用。无论如何,我在这里留下答案,因为如果你的桌子内容有限,它可能是一个解决方案。
有关详细信息,请参阅Shadow on a UITableViewCell disappears when scrolling。
答案 7 :(得分:2)
对于高度为50且行间距为5像素的表格单元格。宽度为320。
定义要清晰的细胞背景:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor clearColor];
}
设置单元格的高度,这是行的大小PLUS分隔符:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 55;
}
在cellForRowAtIndexPath中定义一个框,用行的大小(MINUS分隔符)绘制背景颜色:
UILabel *headerBackgroundLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,320,50)];
backgroundBox.backgroundColor = [UIColor grayColor];
[cell addSubview:backgroundBox];
答案 8 :(得分:1)
我这样做的方式更简单,更灵活。有些人可能称之为黑客攻击。我称之为务实。
我隐藏了标准的UITableViewSeparator。然后我将子视图添加到我的单元格,使用自动布局将其固定到顶部。将高度固定到我想要的高度。将其固定在边缘,两边都有边距。改变它的背景颜色。我有一个普通的分离器,我想要的高度。
您可能会质疑这在单元格层次结构中具有另一个UIView的效率。它真的会产生明显的差异吗?可能不是 - 无论如何,您只是从表层次结构中取出标准分隔符。
答案 9 :(得分:0)
首先使故事板中的表视图分隔符不存在。然后使用情节提要或Xib将UILabel/UIView
添加到高度单元格的底部(您需要)
答案 10 :(得分:0)
这是我找到的最简单的解决方案:
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
" "
}
然后将高度设置为您想要的任何高度:
tableView.sectionHeaderHeight = 30.0
答案 11 :(得分:0)
此问题最简单,最安全的解决方案是关闭表格分隔符,并将UITableViewCell用作可变高度的分隔符。当然,您必须进行一些索引数学运算才能确定项目在哪里,但实际上它是奇/偶。
它不会破裂,您将从可回收单元中受益(无需清理多余的视图)。
答案 12 :(得分:0)
这是一个可能对某些人有用的选项
cell.layer.borderColor = UIColor.white.cgColor
cell.layer.borderWidth = 4.0
cell.layer.masksToBounds = true
答案 13 :(得分:0)
不可能提高默认分隔符。相反,您需要添加一个子视图,该子视图将看起来像每个单元格的分隔符(并且可以选择使该单元格更高)。例如,您可以在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)
}
}
答案 14 :(得分:-1)
对于Swift 4
实施King-Wizard对Swift 4的解决方案:
public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let additionalSeparatorThickness = CGFloat(4)
let additionalSeparator = UIView(frame: CGRect(x: 0,
y: cell.frame.size.height - additionalSeparatorThickness, width: cell.frame.size.width, height: additionalSeparatorThickness))
additionalSeparator.backgroundColor = UIColor.groupTableViewBackground
cell.addSubview(additionalSeparator)
}
答案 15 :(得分:-4)
我遇到的方式让我有效地改变了细胞之间的差距。
在“界面”构建器中,我将行高设置为46。
在我的TableView委托的cellForRowAtIndexPath
方法中,我将单元格的框架设置为较小的值。
cell.frame=CGRectMake(44,0,tableView.bounds.size.width,44)
这给了我一个高度为44的单元格,它符合tableView的宽度,但为行提供的空间将是IB中定义的46。
无论如何我以编程方式填充单元格,所以这很适合我。
答案 16 :(得分:-36)
你应该实施
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
委托方法。然后返回100.0。