gpt4 book ai didi

ios - 为 UITableViewCell 添加不同的子层覆盖 Cell 的 subview

转载 作者:行者123 更新时间:2023-11-28 21:18:48 24 4
gpt4 key购买 nike

当我像这样限制 UITableViewCell 的部分时...

有了这个代码...

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;
}
}
}

在 Tableview 的委托(delegate)方法中,我为每个单元格向 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;

}

但是当我点击任何单元格时,层会覆盖单元格的 subview 如果我错了请设置我的方向

enter image description here

enter image description here

最佳答案

问题可能出在这一行 [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 中设置颜色并将其删除的代码。

关于ios - 为 UITableViewCell 添加不同的子层覆盖 Cell 的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40689777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com