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

时间:2016-03-02 07:34:18

标签: 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];
        }
    }

3 个答案:

答案 0 :(得分:1)

问题

代码没问题,因为它在每个角落都在做角落半径,但问题是你实际上是将视图的原点移动到 y = 12 。视图与tableView具有相同的宽度,并且它具有角落半径,因为视图在右侧的屏幕外显示,所以它没有看到它。

解决方案

要在屏幕中间显示视图,请将其中心设置为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];
}

无论如何,我可以通过在每次单元格出列时不添加该视图来对该代码进行一些改进。另请注意,为此视图设置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

答案 1 :(得分:0)

可能的原因可能是:

你的细胞高度是75.你正在写这个:

UIView * whiteRoundedView = [[UIView alloc]initWithFrame:CGRectMake(0, 12, MaintableView.frame.size.width, 70)];

查看实际的CGRectMake

CGRect CGRectMake ( CGFloat x, CGFloat y, CGFloat width, CGFloat height );

在您的情况下,y = 12.当您放置子视图(您的(0,0,width,height))时,根据单元格的whiteRoundedView,它的超级视图是({1}} 0,12,宽度,高度)其下部不会显示。因为它没有单元格视口。尝试理解这些坐标系。

修改

将您的观看代码更改为:

UIView * whiteRoundedView = [[UIView alloc]initWithFrame:CGRectMake(0, 3, cell.frame.size.width, 69)];

并将拐角半径设置为10.

whiteRoundedView.layer.cornerRadius = 10.0;

运行应用程序并查看差异。

答案 2 :(得分:0)

这是示例代码。 这是你要找的吗?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }
    if ([cell viewWithTag:100] == nil)
    {
        UIView* roundedView = [[UIView alloc]initWithFrame:CGRectInset(cell.bounds, 2, 2)];
        roundedView.backgroundColor = [UIColor lightGrayColor];
        roundedView.tag = 100;
        roundedView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [cell insertSubview:roundedView atIndex:0];
        roundedView.layer.cornerRadius = 5;
        roundedView.layer.masksToBounds = true;
    }

    return cell;
}

在您的情况下,请使用以下内容并删除willdisplaycell

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }
    if ([cell viewWithTag:100] == nil)
    {
        UIView* roundedView = [[UIView alloc]initWithFrame:CGRectInset(cell.bounds, 2, 2)];
        roundedView.backgroundColor = [UIColor lightGrayColor];
        roundedView.tag = 100;
        roundedView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [cell insertSubview:roundedView atIndex:0];
        roundedView.layer.cornerRadius = 5;
        roundedView.layer.masksToBounds = true;
    }
    cell.textLabel.text = @"Hello world";

    return cell;
}

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

    return 75;
}