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

标签 ios objective-c uitableview

您好,我是 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/

相关文章:

ios - 数据不会从 JSON 解析加载到某些 Tableview 单元格上

iOS - 如何使用 Swift 在 UITableview 中以矩阵格式(行和列)显示值

ios - IF 比较 CGFloat 值 - Xcode 7 给出错误

ios - 何时释放为 UICollectionViewCell 光栅化分配的内存?

objective-c - 将 (void *) 转换为 (SInt16 *) 到 Swift

ios - 在公共(public)接口(interface)和私有(private)接口(interface)/实现文件上声明协议(protocol)的区别

ios - 什么叫setSelected :NO method automatically

ios - 如何使 WebView 在 iPhone/iPad Retina 中工作

iOS-MPMoviePlayerController 无法从远程 URL 播放视频

objective-c - UITableView 第一行在顶栏下被截断