推广

【iOS开发】tableView-section圆角边框解决方案

iseeyu2年前 (2024-02-21)推广137

image

找了很多办法都没办法解决。

  • 设置过tableView的边框,但是发现,滑动tableView的时候,其实是里面的content在移动,也就是,向上向下滑,边框并不会动=。=。
  • 可以试着打印tableView的frame,发现frame的x.y并不会移动。

</br>

最后解决了,找了两种方案


【方案一】- 自定义cell

** 圆角边框为UIImage,充当自定义cell的背景图.**

  1. 方法比较简单,就不粘代码了,弄上边圆角,下边圆角,没有圆角,上下边都有圆角四张图片。
  2. 判断加载的是section中的indexpath.row是几,对应加载哪种圆角图片,就可以了。

【方法二】- cell重绘

给section,绘制边框。

  1. 在UITableViewDelegate有个展示cell的代理方法,在展示cell的时候,然后为section中的cell绘制边框
  2. index path.row==0和index path.row==最后一个 的时候,加圆角。
  3. 此方法,可以直接复制粘贴使用,另外,如果此需求用到的地方比较多,可以构建基类或者写在category里面,方便使用
    代码如下:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(tintColor)]) {
//        if (tableView == self.tableView) {
        CGFloat cornerRadius = 10.f;
        cell.backgroundColor = UIColor.clearColor;
        CAShapeLayer *layer = [[CAShapeLayer alloc] init];
        CGMutablePathRef pathRef = CGPathCreateMutable();
        CGRect bounds = CGRectInset(cell.bounds, 10, 0);
        BOOL addLine = NO;
        if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
            CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
        } else if (indexPath.row == 0) {

            CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
            CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
            addLine = YES;

        } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
            CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
            CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
        } else {
            CGPathAddRect(pathRef, nil, bounds);
            addLine = YES;
        }
        layer.path = pathRef;
        CFRelease(pathRef);
        //颜色修改
        layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.5f].CGColor;
        layer.strokeColor=[UIColor blackColor].CGColor;
        if (addLine == YES) {
            CALayer *lineLayer = [[CALayer alloc] init];
            CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
            lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight);
            lineLayer.backgroundColor = tableView.separatorColor.CGColor;
            [layer addSublayer:lineLayer];
        }
        UIView *testView = [[UIView alloc] initWithFrame:bounds];
        [testView.layer insertSublayer:layer atIndex:0];
        testView.backgroundColor = UIColor.clearColor;
        cell.backgroundView = testView;
    }
//    }
}

</br>
参考资料:
http://www.cocoachina.com/bbs/read.php?tid=253277
http://stackoverflow.com/questions/18822619/ios-7-tableview-like-in-settings-app-on-ipad

转自:https://www.jianshu.com/p/da1adec5a601

扫描二维码推送至手机访问。

版权声明:本文由西安泽虎代运营发布,如需转载请注明出处。

转载请注明出处https://www.0291.com.cn/post/56767.html

相关文章

想要稳定关键词排名。

想要稳定关键词排名。

在文章发表期间,SEO工作人员很少讨论这个问题。我们知道,搜索引擎喜欢新鲜优质的内容,随着时间的推移,它们会逐渐降低旧新闻的排名。这使得一些SEO专家试图修改文章发布日期,以提高相关内容的排名。这是提高相关内容排名的好方法吗?它对搜索引擎有什么影响? 那么,发布日期对SEO有什么影响? 经过测试...

淘宝认证后怎么开店(淘宝开店认证已通过是不是已经开店成功了)

淘宝认证后怎么开店(淘宝开店认证已通过是不是已经开店成功了)

不需要将详情页做的多么华丽,但需要突出产品的特点和卖点。不要进行大段的文字描述,尽量用图片来体现产品。要写好退换货要求和流程,让消费者可以放心购买。...

中小型企业选择网络营销的优势。

中小型企业选择网络营销的优势。

点瑞网络是一家主营:SEO,网站优化,关键词排名优化,百度搜索引擎优化,整站优化服务公司,目前已服务数百家企业,精于数据分析,整合推广,流量排名提升,转化率提升!是您开展网络营销推广的不二之选! 一、网络营销是没有时间约束,内容详尽,并且可以选择多家适合的行业媒体进行报道传送。网络营销带来的...

淘宝客定向计划佣金设置多少,淘宝客佣金率多少合适(淘宝客营销计划佣金怎么改)

淘宝客定向计划佣金设置多少,淘宝客佣金率多少合适(淘宝客营销计划佣金怎么改)

定向推广计划是卖家为淘宝客中某一个细分群体设置的推广计划,可以让淘宝客在阿里妈妈前台看到推广并吸引淘宝客参加。当然,卖家们也可以去和一些大的网站协商好,不公开的形式,然后可以让让卖家获取较大的流量,同时淘宝客也是有较高的佣金。...

道指暴跌超1000点!鲍威尔“吓崩”美股 美国富豪财富一夜蒸发 ...

道指暴跌超1000点!鲍威尔“吓崩”美股 美国富豪财富一夜蒸发 ...

一场演讲彻底“吓崩”了美股。北京时间8月26日晚间,美联储主席鲍威尔就美国经济前景发表了演讲。鲍威尔演讲中明确表态,美联储不会被一两个月的所左右,当前美国的通胀形势依旧严峻,美联储必须继续加息。同时,鲍威尔直接反驳了货币对2023年下半年开始降息的定价。这番表态后,市场对9...

网站速度优化的影响因素有哪些?从哪些方面提升网站速度。

网站速度优化的影响因素有哪些?从哪些方面提升网站速度。

网站速度优化的影响因素有哪些?网站的速度是非常重要的。如果打开网站需要3秒钟以上的时间,通常人们会直接关闭网站,这意味着大部分客户都会被拒之门外。互联网的速度不够快,这也影响了客户的体验。它需要优化。让我们看看在优化网站速度时的一些影响因素。 做好网站内容优化。构建内容时,您希望在网...

现在,非常期待与您的又一次邂逅

我们努力让每一部企业宣传片和抖音短视频成为商业大片