简体   繁体   English

如何在iOS中对表格视图单元进行四舍五入

[英]How to round the table view cells in ios

i want to round table-list cells either bottom or top side but not both sides can anybody please help me with example code and i have done like below but it round the both side edges but i want round only one side according to requirement(either top or bottom) 我想在底部或顶部对表列表单元进行圆整,但不是两侧都可以有人帮助我提供示例代码,我已经完成了如下所示的操作,但是它对两侧边缘进行了圆整,但是我只想根据需要对一侧进行圆整(上面或者下面)

 [cell.layer setCornerRadius:7.0f];
  [cell.layer setMasksToBounds:YES];
  [cell.layer setBorderWidth:2.0f];

This above code rounds both sides cells edges but i want round at one side edge please help me somebody 上面的代码将两边的单元格边缘都弄圆了,但是我想在一侧边上弄圆,请帮助我

You can't actually create a semi-circle shaped UIView or UITableViewCell like that unless you jump into core graphics yourself. 除非您自己跳入核心图形,否则实际上不能创建像这样的半圆形UIView或UITableViewCell。

A simple hack that I would recommend is to have a UIView in the cell, with all the components insides the UIView . 我建议的一个简单技巧是在单元格中包含一个UIView ,并将所有组件都包含在UIView Now make that UIView into a round shape, and give the UIView a double height of the cell itself. 现在,使该UIView变为圆形,并为该UIView提供单元格自身高度的两倍。 So if your cell is of height 44, make the UIView with a height of 88. This way half of the UIView will get clipped because it won't be able to show in the cell itself. 因此,如果您的单元格高度为44,则将UIView的高度设置为88。这样一来,一半的UIView将会被裁剪,因为它无法在单元格本身中显示。

And you will end up with a half part of a circle visible in a UITableViewCell . 最后,您将在UITableViewCell看到一个圆圈的一半。

Hope this helps you out. 希望这可以帮助你。 Please let me know if you have any trouble with it. 如果您有任何麻烦,请告诉我。

Update: 更新:

To achieve this, you can use a code similar to the following one: 为此,可以使用类似于以下代码的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    [cell.contentView setClipsToBounds:YES];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(5,5,(cell.frame.size.height*2.0),(cell.frame.size.height*2.0))];
    view.backgroundColor = [UIColor whiteColor];
    [view.layer setCornerRadius:view.frame.size.height/2];
    [view setClipsToBounds:YES];
    [cell addSubview:view];
    return cell;
}

this can be achieve through make image with top side round corner. 这可以通过使图像具有顶侧圆角来实现。 set that image as a background image and set cell-background color as a clear color 将该图像设置为背景图像,并将单元格背景色设置为透明色

in your Code apply this Code , i have tested , its working perfectly. 在您的代码中应用此代码(我已经测试过),它可以正常运行。

maskPath1 = (UIImageView *)[self roundCornersOnView: maskPath1 onTopLeft:YES topRight:YES bottomLeft:NO bottomRight:NO radius:20.0];

//use below Method to Set Corner Radius Round.. //使用以下方法设置转角半径圆角。

-(UIView *)roundCornersOnView:(UIView *)view onTopLeft:(BOOL)tl topRight:(BOOL)tr bottomLeft:(BOOL)bl bottomRight:(BOOL)br radius:(float)radius {

    if (tl || tr || bl || br) {
        UIRectCorner corner = 0; //holds the corner
        //Determine which corner(s) should be changed
        if (tl) {
            corner = corner | UIRectCornerTopLeft;
        }
        if (tr) {
            corner = corner | UIRectCornerTopRight;
        }
        if (bl) {
            corner = corner | UIRectCornerBottomLeft;
        }
        if (br) {
            corner = corner | UIRectCornerBottomRight;
        }

        UIView *roundedView = view;
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = roundedView.bounds;
        maskLayer.path = maskPath.CGPath;
        roundedView.layer.mask = maskLayer;
        return roundedView;
    } else {
        return view;
    }

}

///////////// For TableVIew Use Below Code in cellForRowAtIndexPath Method ///// //////////////////////////////

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.Img_thumb=(UIImageView *)[self roundCornersOnView: cell.Img_thumb onTopLeft:YES topRight:YES bottomLeft:NO bottomRight:NO radius:20.0];
}

Check Screen Shot :-> 检查屏幕截图:-> 查看屏幕截图,您可以看到圆角

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM