Mohammed Hussain Mohammed Hussain - 13 days ago 10
iOS Question

Adding Different sublayers to UITableViewCell Covering subviews of Cell

As I'm bounding the section of

UITableViewCell
like this...

With this Code...

enter image description here

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

if (indexPath.section == 0) {
//Border Across 1st Section
if ([cell respondsToSelector:@selector(tintColor)]) {
CGFloat cornerRadius = 3.f;
cell.backgroundColor = UIColor.clearColor;
self.layer = [[CAShapeLayer alloc] init];
CGMutablePathRef pathRef = CGPathCreateMutable();
CGRect bounds = CGRectInset(cell.bounds, 10, 0);
BOOL addLine = NO;
if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
} else if (indexPath.row == 0) {
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
addLine = YES;
} else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
} else {
CGPathAddRect(pathRef, nil, bounds);
addLine = YES;
}
self.layer.path = pathRef;
CFRelease(pathRef);
//set the border color
self.layer.strokeColor = UNDERLINE_COLOR_NEXT.CGColor;
//set the border width
self.layer.lineWidth = 1;
self.layer.fillColor = [UIColor colorWithWhite:1.f alpha:1.0f].CGColor;

if (addLine == YES) {
CALayer *lineLayer = [[CALayer alloc] init];
CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
lineLayer.frame = CGRectMake(CGRectGetMinX(bounds), bounds.size.height-lineHeight, bounds.size.width, lineHeight);
lineLayer.backgroundColor = tableView.separatorColor.CGColor;
[self.layer addSublayer:lineLayer];
}

UIView *testView = [[UIView alloc] initWithFrame:bounds];
[testView.layer insertSublayer:self.layer atIndex:0];
testView.backgroundColor = UIColor.clearColor;
cell.backgroundView = testView;
}
}
}


And in Tableview's delegate method I'm adding sublayer to
CAShapeLayer
for each cell...like this...

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.view endEditing:YES];

if (indexPath.section == 0) {

UITableViewCell *lCell = [tableView cellForRowAtIndexPath:self.lastIndexPath];
CALayer *lLayer = [[CALayer alloc]init];
lLayer.frame = CGRectInset(lCell.bounds,10,0);
lLayer.backgroundColor = [UIColor colorWithWhite:1.f alpha:1.f].CGColor;
[lCell.layer addSublayer:lLayer]; //Last indexPath white

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CALayer *cLayer = [[CALayer alloc]init];
cLayer.frame = CGRectInset(cell.bounds,10,0);
cLayer.backgroundColor = navBarColor.CGColor;
[cell.layer addSublayer:cLayer]; //Current Index Yellow

self.lastIndexPath = indexPath;

}


But when i'm tapping on any cell, layer is covering subview of cell Were i'm wrong please set my direction

enter image description here

enter image description here

Answer

first your cell background view, give tag to identify that like this way

must add this line

 testView.tag = 9999;

it's look like

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  UIView *testView = [[UIView alloc] initWithFrame:bounds];
  [testView.layer insertSublayer:self.layer atIndex:0];
  testView.backgroundColor = UIColor.clearColor;
  testView.tag = 9999;
  cell.backgroundView = testView;

}

now implement didSelectRow method is like this way

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.view endEditing:YES];

if (indexPath.section == 0) {

    UITableViewCell *lCell = [tableView cellForRowAtIndexPath:self.lastIndexPath];
    UIView *bgView = [[lCell backgroundView] viewWithTag:9999]
    CAShapeLayer *layer = (CAShapeLayer *)bgView.layer.sublayers[0];
    layer.fillColor = [UIColor colorWithWhite:1.f alpha:1.f].CGColor //Last indexPath white


    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIView *bgView = [[cell backgroundView] viewWithTag:9999]
    CAShapeLayer *layer = (CAShapeLayer *)bgView.layer.sublayers[0];
    layer.fillColor = navBarColor.CGColor  //Current Index Yellow


    self.lastIndexPath = indexPath;

}
}
Comments