With my knowledge two possibilites come to my mind to solve this:
Instead of using the default accessory views, use no accessory view at all. Instead fake the accessory views by creating the images needed for the acessory views (screenshot of retina display or using a custom image or drawing itself). If you have your fake accessory view one can add it to the contentView
and position it with constraints accordingly.
Now you can add a line to the content view while disabling the default separator with TableView.SeparatorStyle = UITableViewCellSeparatorStyle.None;
. But you have to add events to the created UIButton
which holds the image because AccessoryButtonTapped
will not be called anymore. That are many steps only for adding a separator line.
Add the separator line on the background view. This is the approach I took.
First the GradientView
class which creates my gradient background view with my separator lines:
public class GradientView : UIView
{
// accessors
private CAShapeLayer line;
private bool shouldShowSeparatorLine = false;
private CAGradientLayer gradientLayer {
// read-only
get { return (CAGradientLayer)this.Layer; }
}
public CGColor[] Colors {
// set the colors of the gradient layer
get { return this.gradientLayer.Colors; }
set { this.gradientLayer.Colors = value; }
}
public GradientView(bool shouldShowSeparatorLine = false)
{
this.shouldShowSeparatorLine = shouldShowSeparatorLine;
this.BackgroundColor = UIColor.Clear;
}
[Export ("layerClass")]
public static Class LayerClass ()
{
// use a different Core Animation layer for its backing store
// normally a CALayer is used for a UIView
return new Class (typeof(CAGradientLayer));
}
public override void Draw (RectangleF rect)
{
base.Draw (rect);
if (shouldShowSeparatorLines) {
// get graphics context
CGContext context = UIGraphics.GetCurrentContext ();
context.SetStrokeColor(UIColor.FromRGB (21,66,139).CGColor);
context.SetLineWidth (1.0f);
context.SetShouldAntialias (false);
float top = 0;
if (Util.UserInterfaceIdiomIsPhone) {
top = 0;
} else {
top = 1;
}
// top
// start point
context.MoveTo (0, top);
// end point
context.AddLineToPoint (rect.Width, top);
// draw the path
context.DrawPath (CGPathDrawingMode.Stroke);
// bottom
// start point
context.MoveTo (0, rect.Height);
// end point
context.AddLineToPoint (rect.Width, rect.Height);
// draw the path
context.DrawPath (CGPathDrawingMode.Stroke);
}
}
You don't need two separator lines, but for my selection problem I needed both.
In your custom UITableViewCell
you set the background in the initializer accordingly:
GradientView background = new GradientView (true);
background.Colors = new CGColor[] {
UIColor.White.CGColor,
UIColor.White.CGColor,
UIColor.Blue.CGColor
};
this.BackgroundView = background;
Of course you don't need the gradient thing so you can leave it out. Than your GradientView
will only contain the Draw
methow with a few fields.