如何在iPhone中自定义tableView分隔符

默认情况下,在uitableview中有一个单独的行分隔符。

但是我想把我的自定义的行作为分隔符。

可能吗? 怎么样?

如果你想做的不仅仅是使用UITableView的separatorColor属性改变分隔符的颜色,那么你可以设置separatorStyle属性为UITableViewCellSeparatorStyleNone ,然后:

  • 创build自定义UITableViewCell ,其中包括您的自定义分隔符
  • 创build包含您的自定义分隔符的备用UITableViewCell

例如,如果您的表格当前显示5行,则可以更新它以显示9行,并且索引1,3,5,7处的行将是分隔符单元格。

有关如何创build自定义UITableViewCell的更多信息,请参阅Table View Programming Guide中的UITableViewCell子类 。

更好的解决scheme是使用单元格的当前宽度和高度。 像这样的东西:

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.contentView.frame.size.height - 1.0, cell.contentView.frame.size.width, 1)]; lineView.backgroundColor = [UIColor darkGrayColor]; [cell.contentView addSubview:lineView]; 

如果不同的行需要不同的分隔符颜色,或者希望分隔符在该行突出显示时保持可见,请尝试以下操作:

 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // We have to use the borderColor/Width as opposed to just setting the // backgroundColor else the view becomes transparent and disappears during // the cell's selected/highlighted animation UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 1024, 1)]; separatorView.layer.borderColor = [UIColor redColor].CGColor; separatorView.layer.borderWidth = 1.0; [cell.contentView addSubview:separatorView]; 

这假定你的单元格的背景颜色是透明的。


上面的解决scheme出来了一些广泛的实验。 以下是关于我的发现的一些说明,我确定会帮助人们:

在正常的“未select”状态

  • contentView(你的XIB中的什么,除非你另外编码)是正常绘制的
  • selectedBackgroundView是HIDDEN
  • backgroundView是可见的(所以你的contentView是透明的,你可以看到backgroundView或者(如果你没有定义backgroundView,你会看到UITableView本身的背景色)

一个单元格被选中,下列情况立即出现:-out任何animation:

  • contentView中的所有视图/子视图都清除了backgroundColor(或设置为透明),标签等文本颜色更改为其所选颜色
  • selectedBackgroundView变得可见(这个视图总是单元格的大小(自定义框架被忽略,如果需要的话使用子视图)还要注意,由于某种原因,子视图的backgroundColor不会显示,也许它们被设置为透明像contentView)。 如果你没有定义selectedBackgroundView,那么Cocoa会创build/插入蓝色(或灰色)渐变背景并显示给你)
  • backgroundView没有改变

当单元格被取消select时,开始删除突出显示的animation:

  • selectedBackgroundView alpha属性是从1.0(完全不透明)到0.0(完全透明)的animation。
  • backgroundView再次不变(所以animation看起来像selectedBackgroundView和backgroundView之间的交叉淡入淡出)
  • 只有animation完成后,contentView才会被重画为“未选中”状态,其子视图backgroundColor会再次变为可见(这会导致animation看起来很糟糕,所以build议您不要在您的UIView.backgroundColor中使用内容查看)

这些答案导致高亮直接被您添加到您的单元格的分隔符(至less在我的testingiOS 6上)覆盖。 您需要将separatorColor设置为[UIColor clearColor]以便单元格仍然以1px分隔,然后可以在间隙中绘制分隔符:

 - (void)viewDidLoad { self.tableView.separatorColor = [UIColor clearColor]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // snip CALayer *separator = [CALayer layer]; separator.backgroundColor = [UIColor redColor].CGColor; separator.frame = CGRectMake(0, 43, self.view.frame.size.width, 1); [cell.layer addSublayer:separator]; return cell; } 

添加tableView的以下代码cellForRowAtIndexPath委托可以为每个单元格创build1px高度和200px宽度的自定义图像视图

 UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, 1)]; lineView.backgroundColor = [UIColor blackColor]; [cell.contentView addSubview:lineView]; 

注意 :我不知道它的performance有多沉重。

我不知道这是否可以通过一些设置“自动”完成。 但build议将行分隔符设置为none,并在您的单元格的边界实际上绘制您所需的行分隔符。

如果您在Swift中使用自定义单元格:另一种方法是使用可以在该单元格的顶部绘制一条线的函数来扩展UITableViewCell。

 import UIKit extension UITableViewCell { func addSeparatorLineToTop(){ var lineFrame = CGRectMake(0, 0, bounds.size.width, 1) var line = UIView(frame: lineFrame) line.backgroundColor = UIColor.myGray_300() addSubview(line) } 

}

然后你可以添加这行到任何自定义单元格,例如在awakeFromNib中

 override func awakeFromNib() { super.awakeFromNib() addSeparatorLineToTop() } 

这个解决scheme是很好的,因为它不会搞乱你的故事板,并限制你的额外代码为1行。

在视网膜显示器上,由于抗锯齿效应,即使画出0.5单位线,也会产生双像素线。 要在两个显示器上将其渲染为单个像素,请将其上移四分之一的单位:

 UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.contentView.frame.size.height - (1.0 - 0.25), self.contentView.frame.size.wi lineView.backgroundColor = [UIColor colorWithRed:(230.0/255.0f) green:(233/255.0f) blue:(237.0/255.0f) alpha:1.0f]; [self.contentView addSubview:lineView]; 

在iOS 7(GM)上testing:

 @implementation MyTableViewController - (void)viewDidLayoutSubviews { for (UIView *view in self.view.subviews) { if ([view isKindOfClass:NSClassFromString(@"_UITableViewCellSeparatorView")]) view.backgroundColor = [UIColor redColor]; } } @end 

注意:看起来UITableView 分隔符移动到某些configuration中的单元格,这将导致此代码无法工作,除非您也下载到所有的UITableViewCells中。

下面要点中的单元格是一个UITableViewCell的子类,每个单元格可以有自己的分隔符,包括多种样式(目前仅支持.None和.Default)。 它是用Swift编写的,假定使用Autolayout。

https://gist.github.com/evgeniyd/fa36b6f586a5850bca3f

如何使用这个类:

  1. 将UITableView对象的分隔符样式设置为UITableViewCellSeparatorStyle.None

     tableView.separatorStyle = .None 
  2. 创buildMPSTableViewCell的子类

  3. awakeFromNib()里的某个地方设置单元格的分隔符样式

注意:下面的代码假定单元格是从xib / storyboard加载的

  class FASWorkoutCell: FASTableViewCell { required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override func awakeFromNib() { super.awakeFromNib() separatorType = .Default } // ... } 

如果您使用自定义的UITableViewCell,则只需在UItableViewCell.xib的底部添加UIView,然后将背景色作为所需的颜色。

例如,在xib上,我在底部添加一个UIView,并将高度设置为1.使用自动布局,我将left约束设置为12,bottom约束设置为0,right约束为0,height设置为1。

Swift版本:

 private let kSeparatorTag = 123 private let kSeparatorHeight: CGFloat = 1.5 func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { if cell.viewWithTag(kSeparatorTag) == nil //add separator only once { let separatorView = UIView(frame: CGRectMake(0, cell.frame.height - kSeparatorHeight, cell.frame.width, kSeparatorHeight)) separatorView.tag = kSeparatorId separatorView.backgroundColor = UIColor.redColor() separatorView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] cell.addSubview(separatorView) } }