推广

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

iseeyu3年前 (2024-02-21)推广141

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

相关文章

五种营销理念,哪种适合你的企业?

五种营销理念,哪种适合你的企业?

在现代商业史上,理念一直在演变,前后持续了一百多年,从最初的产品导向,再到销售导向,之后发展到顾客导向和社会营销导向。生产导向历史上最早的营销理念——生产导向(Production Concept)。生产导向认为,消费者最关心的是产品价格,因此企业的核心是提高生产效率和降低...

新站该怎么做百度系SEO呢。

新站该怎么做百度系SEO呢。

关于许多站长做大搜仍是许多人前赴后继在进入这个圈子,每天学SEO的人全国这些在线训练和线下训练简直几十万人。这么大的集体都在做大搜,你作为站长为啥不去尽力呢,最近有内部消息说今天头条预备布局查找职业。 许多网站都现已有今天头条的蜘蛛在爬去网站。这就阐明,大搜今后不是百度一家的天下了,...

区域母婴连锁将呈寡头效应?供应链整合竞争力更强大

历经20多年发展,今天行业已经脱颖而出一批优秀母婴连锁。前两年众多业内人士提出:母婴店规模化、连锁化将是发展大势。走访众多下线后,笔者发现四川这种趋势已经初具模型。四川大多地区连锁“称王”,与河南、安徽大有差别从区域市场来看,在四川绵阳、眉山、雅安、德阳、乐山、内江、宜宾、...

奇瑞瑞虎3Xe安全维修开关拆装,高压断电下电

奇瑞瑞虎3Xe安全维修奇瑞瑞虎3xe纯电动车型,高压安全维修开关在中央马鞍座换挡旋钮下方。奇瑞瑞虎3Xe安全维修拆卸时需先关闭点火开关,拆除12伏特低压蓄电池负极,收藏好钥匙,等待5分钟以上,拆除换挡器总成,拆开中央马鞍座,然后,穿戴安全绝缘手套,在有专业人士看护的情况下,...

百度竞价推广中的冷门词应该如何处理?

百度竞价推广中的冷门词应该如何处理?

百度竞价中冷门词应该如何处理?什么是百度竞价冷门词?在百度推广后台,是这样说明的:“冷门词是指30天内在百度搜索无任何检索量的关键词,这些词无检索量,通常是因为太长、太生僻、不符合网民表达习惯,或季节过时等原因。”上边说得很清楚,但要注意的是:有些词在现在这个时间段是冷门词...

淘宝店铺装修多少钱一个月(网店店铺装修要多少钱)

淘宝店铺装修多少钱一个月(网店店铺装修要多少钱)

如果是标准版的旺铺,一般一年是360元,也可以按月来支付,每月的费用是30元,除此之外,店铺装修也是个费钱的项目,我们可以用模板自己装,也可以请专业的美工来装。...

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

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