推广

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

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

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://0291.com.cn/post/56767.html

相关文章

什么是软文推广?软文推广有什么好处?

什么是软文?想了解软文推广,首先要了解什么是软文。软文,是相对于在文章中直接生硬的植入广告的一种关广告性质文章形式。狭义上,软文相当于企业在门户网站投放的一种纯文字广告;广义上,软文相当于企业在以网络为载体的平台上所推广的宣传性文章,用以打响企业的形象、知名度,提高公司的产...

我来教你杭州网站设计—如何做到网页设计的精美、布局合理。

我来教你杭州网站设计—如何做到网页设计的精美、布局合理。

      前段时间一个朋友推荐我一款游戏,名是“王者荣耀”,特别的推荐我说这是一款如何如何好的游戏,反正吹的我认为挺夸张的,就对这款游戏有了好奇心,有了一点小期待,怀着这样的心情去下载了。         点开这款游戏,我...

软文如何写才能高效获客?一键掌握密码!

在互联网席卷商业之前,对于很多商贩来说,获客似乎并不是什么难事。过去集市上的商贩大概无法理解现代企业老板的获客焦虑,东西就在这里,我也花了大价钱宣传了,怎么就没有人来呢?就当今来看,开拓获客对中小企业整体发展的重要性不言而喻。许多企业在推行硬广、竞价等方面投入颇多,但最终成...

网站SEO优化的传统思维转变

网站SEO优化的传统思维转变

经历过熊掌号在 2018 年的迅猛发展,再到年底淡出甚至开始销声匿迹,2019 年的 SEO 优化该何去何从?是继续回归内容为王,链接为皇?还是继续做快排,做着大众都熟知的 SEO 优化上下功夫呢? 在去年深深的经历过熊掌号从爆发到淡出,享受过网站优化中从未有过的那种舒适感,可说是在过去...

教你2022年的4种创新网站设计趋势。

教你2022年的4种创新网站设计趋势。

在不断变化。设计师在极限中发挥作用,重塑以前的风格,并不断尝试新技术。虽然有些流行的风格不会消失,如简约主义和平面插图,但新的设计趋势也会不断涌现,2022年网站设计都有哪些创新的趋势呢? 小编科技是位于杭州的一家高端的网站制作、网站设计公司,拥有专业的网站设计团队,时刻关注网站设计发展...

为什么用户点击了你的游戏信息流广告,却没有转化?

为什么用户点击了你的游戏信息流广告,却没有转化?

  那些真实用户没有转化的原因,可能跟你的落地页有关。   01 前言 本文只讨论有落地页的情况,不包括应用下载和商店直达。   02 问题分析 在排除掉机器点击、同行点击、老用户点击、用户误点、流量劫持、激活延迟、自然量归因这些大家都会遇到的“...

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

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