正如我UITableViewCell
这样的部分一样......
使用此代码......
- (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;
}
}
}
在Tableview的委托方法中,我为每个单元格添加了CAShapeLayer
的子图层......就像这样......
#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;
}
但是,当我点击任何一个单元格时,图层覆盖了单元格的子视图我错了,请设置我的方向
答案 0 :(得分:1)
问题可能在于此行[self.layer addSublayer:cLayer];
您需要在单元格中添加图层。它应该是。
[cell.layer addSublayer:cLayer];
您需要为当前和之前的选择设置[cell.layer addSublayer:cLayer];
。
修改强>
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
UIView *selectedView = [[UIView alloc] initWithFrame:cell.bounds];
selectedView.backgroundColor = navBarColor.CGColor;
cell.selectedBackgroundView = selectedView;
}
现在删除您正在设置颜色并删除它的didSelect
内的代码。
答案 1 :(得分:1)
首先是您的单元格背景视图,给出标记以便像这样识别
必须添加此行
testView.tag = 9999;
看起来像
- (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;
}
现在实现didSelectRow方法就是这样
- (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;
}
}
答案 2 :(得分:0)
如果你要做的只是改变当前所选单元格的颜色,那么有一个更简单的解决方案。您可以做的是将所有UITableViewCell
的突出显示状态设置为单独的UIView
。只需单击单元格中的插座,然后将连接从UITableViewCell
拖放到UIView
(查看要从中拖出的插座的屏幕截图)。这将设置所选单元格以在您设置的视图上进行,从而更改背景颜色,使单元格内的所有元素保持不变。通过这种方式,您不必处理由于细胞重复使用而可能出现的任何颜色变化等
答案 3 :(得分:0)
- (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;
}
}