繁体   English   中英

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

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

我想在底部或顶部对表列表单元进行圆整,但不是两侧都可以有人帮助我提供示例代码,我已经完成了如下所示的操作,但是它对两侧边缘进行了圆整,但是我只想根据需要对一侧进行圆整(上面或者下面)

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

上面的代码将两边的单元格边缘都弄圆了,但是我想在一侧边上弄圆,请帮助我

除非您自己跳入核心图形,否则实际上不能创建像这样的半圆形UIView或UITableViewCell。

我建议的一个简单技巧是在单元格中包含一个UIView ,并将所有组件都包含在UIView 现在,使该UIView变为圆形,并为该UIView提供单元格自身高度的两倍。 因此,如果您的单元格高度为44,则将UIView的高度设置为88。这样一来,一半的UIView将会被裁剪,因为它无法在单元格本身中显示。

最后,您将在UITableViewCell看到一个圆圈的一半。

希望这可以帮助你。 如果您有任何麻烦,请告诉我。

更新:

为此,可以使用类似于以下代码的代码:

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

这可以通过使图像具有顶侧圆角来实现。 将该图像设置为背景图像,并将单元格背景色设置为透明色

在您的代码中应用此代码(我已经测试过),它可以正常运行。

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

//使用以下方法设置转角半径圆角。

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

}

//////////////////////////////

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

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

暂无
暂无

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

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