几个想法:
首先,我认为使用情节提要使子类化UITableViewCell
更加容易。加载 NIB 无需代码。您在适当的位置设计子类单元格。
其次,我会考虑使用Quartz 2D以编程方式配置单元格的内边框。Quartz 2D 具有虚线、阴影等功能。
基本上,您可以通过编程方式调整您的用户界面。因此,将 添加QuartzCore.framework
到您的项目中,并为单元格的内边框创建一个子类,可能类似于:
#import <QuartzCore/QuartzCore.h>
@interface CellInnerBorderView ()
@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@end
@implementation CellInnerBorderView
// this adds shadow and border to the cell
- (void)configureBackgroundView
{
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowRadius = 2.0;
self.layer.shadowOpacity = 0.8;
self.layer.shadowOffset = CGSizeMake(0.0, 2.0);
self.layer.borderColor = [UIColor blackColor].CGColor;
self.layer.borderWidth = 1.0;
}
// this adds a dashed line, half way down, inset by 5 points left and right
- (void)addDashedSeparator
{
UIBezierPath *path = [UIBezierPath bezierPath];
CGPoint startPoint = CGPointMake(5.0, self.frame.size.height / 2.0);
CGPoint endPoint = CGPointMake(self.frame.size.width - 10.0, self.frame.size.height / 2.0);
[path moveToPoint:startPoint];
[path addLineToPoint:endPoint];
if (self.shapeLayer)
[self.shapeLayer removeFromSuperlayer];
self.shapeLayer = [CAShapeLayer layer];
self.shapeLayer.path = path.CGPath;
self.shapeLayer.strokeColor = [UIColor blackColor].CGColor;
self.shapeLayer.lineDashPattern = @[@1, @1];
self.shapeLayer.fillColor = [UIColor blackColor].CGColor;
self.shapeLayer.lineWidth = 1.0;
self.shapeLayer.strokeStart = 0.0;
self.shapeLayer.strokeEnd = 1.0;
[self.layer addSublayer:self.shapeLayer];
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self configureBackgroundView];
[self addDashedSeparator];
}
@end
然后,您可以在 Interface Builder 中添加一个表示UITableViewCell
内部边框的视图,指定它的类CellInnerBorderView
(或您选择的任何名称),然后您将获得此行为。