gpt4 book ai didi

ios - 如何在 ios 中为 TableView 单元格设置圆角半径

转载 作者:行者123 更新时间:2023-11-29 12:03:55 37 4
gpt4 key购买 nike

您好,我是 iOS 的新手,在我的应用程序中,我正在使用 TableList,我想将 tableList 单元角半径设置为 3.0。这是我的代码。它仅在左角而不是所有角上应用角半径。

我的代码:-

- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

return 75;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *simpleTableIdentifier = @"MyCell";

Cell = (MyTripsCell *)[MaintableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (Cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyTripsCell" owner:self options:nil];
Cell = [nib objectAtIndex:0];
}

Cell.layer.cornerRadius = 0.0;
Cell.selectionStyle = UITableViewCellSelectionStyleNone;

return Cell;
}


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

cell.contentView.backgroundColor = [Bg colorWithHexString:@"EEEEEE"];
UIView * whiteRoundedView = [[UIView alloc]initWithFrame:CGRectMake(0, 12, MaintableView.frame.size.width, 70)];

whiteRoundedView.layer.backgroundColor = [Bg colorWithHexString:@"FDFDFD"].CGColor;

whiteRoundedView.layer.masksToBounds = YES;
whiteRoundedView.layer.cornerRadius = 3.0;
whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, -1);
[cell.contentView addSubview:whiteRoundedView];
[cell.contentView sendSubviewToBack:whiteRoundedView];

if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}

最佳答案

问题

代码没问题,因为它在每个角都设置了角半径,但问题是您实际上是将 View 的原点移动到 y=12。该 View 与 tableView 具有相同的宽度,并且它具有您只是看不到的角半径,因为 View 在右侧超出了屏幕。

解决方案

为了让您的 View 位于屏幕中间,您将其中心设置为 contentView 的中心。并使用宽度来设置填充。

您的代码解决方案:

// Using this width and setting the view to the center will give you padding of 12 pixels on left and right
NSInteger width = MaintableView.frame.size.width - 24;
UIView * whiteRoundedView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width, 70)];
whiteRoundedView.center = cell.contentView.center;

whiteRoundedView.layer.backgroundColor = [Bg colorWithHexString:@"FDFDFD"].CGColor;

whiteRoundedView.layer.masksToBounds = YES;
whiteRoundedView.layer.cornerRadius = 3.0;
whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, -1);
[cell.contentView addSubview:whiteRoundedView];
[cell.contentView sendSubviewToBack:whiteRoundedView];

if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}

无论如何,我可以通过不在每次单元格出队时都添加该 View 来对该代码进行一些改进。另请注意,为此 View 设置 maskToBounds = YES 会导致您的阴影不显示(参见 here )

我的代码解决方案(已编辑)

UIView *roundedView = [cell.contentView viewWithTag:100];
if (roundedView == nil) {
// Add some padding to the view in order to see the shadow
NSInteger spacingBothHorizontal = 2;
CGRect customizedFrame = CGRectMake(spacingBothHorizontal/2, 0, CGRectGetWidth(cell.contentView.frame) - spacingBothHorizontal, CGRectGetHeight(cell.contentView.frame) - 20);
roundedView = [[UIView alloc] initWithFrame:customizedFrame];

// Layer customizations
roundedView.layer.cornerRadius = 10.0f;
roundedView.backgroundColor = [UIColor redColor];
roundedView.layer.shadowOffset = CGSizeMake(-1, -1);
roundedView.layer.shadowOpacity = 2.0;
roundedView.layer.shadowColor = [UIColor blueColor].CGColor;
roundedView.tag = 100;

if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}

// Add it to view
[cell.contentView addSubview:roundedView];
[cell.contentView sendSubviewToBack:roundedView];
cell.contentView.backgroundColor = [UIColor blackColor];
}

结果

enter image description here

关于ios - 如何在 ios 中为 TableView 单元格设置圆角半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35741062/

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