Skip to content

Instantly share code, notes, and snippets.

@kaiix
Created November 14, 2012 08:20
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kaiix/4070967 to your computer and use it in GitHub Desktop.
Save kaiix/4070967 to your computer and use it in GitHub Desktop.
Add dotted/dashed border to a UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
// style
cell.textLabel.textColor = [UIColor colorWithRed:65.0/255.0
green:131.0/255.0
blue:196.0/255.0
alpha:1.0];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
CAShapeLayer *shapelayer = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPath];
//draw a line
[path moveToPoint:CGPointMake(0.0, cell.frame.size.height)]; //add yourStartPoint here
[path addLineToPoint:CGPointMake(cell.frame.size.width, cell.frame.size.height)];// add yourEndPoint here
UIColor *fill = [UIColor colorWithRed:0.80f green:0.80f blue:0.80f alpha:1.00f];
shapelayer.strokeStart = 0.0;
shapelayer.strokeColor = fill.CGColor;
shapelayer.lineWidth = 1.0;
shapelayer.lineJoin = kCALineJoinRound;
shapelayer.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:3 ], nil];
// shapelayer.lineDashPhase = 3.0f;
shapelayer.path = path.CGPath;
[cell.contentView.layer addSublayer:shapelayer];
// set content
cell.textLabel.text = [self getContentForRow:indexPath.row];
return cell;
}
@thehalvo
Copy link

thehalvo commented Mar 2, 2014

This was exactly what I needed, thanks!

@jesus-sayar
Copy link

Perfect! Thanks ;-)

@KarthikMandava
Copy link

KarthikMandava commented Nov 22, 2017

I am using this librarycode in tableview cell which is dynamic height and it's working fine as expected, but when playing with scroll of tableview view the dashed in is disturbing. Please check these images.
screen shot 2017-11-22 at 4 12 28 pm
screen shot 2017-11-22 at 4 12 52 pm

@StoneCX
Copy link

StoneCX commented Mar 21, 2022

thanks for the code, but this dotline can't be written in here. that's will cause strange bugs .the better way is written in cell's class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment