为tableView中的一组cell添加边框

CGFloat cornerRadius = 5.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.8f].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;

原理是根据条件判断贝塞尔曲线的画线路径,这段代码放到willDisplayCell的代理方法中即可,然后根据需求调整边框样式..

效果图如下:

效果图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将combobox、qmodel添加到一个tableview,可以使用QItemDelegate类来自定义单元格显示和编辑方式。具体步骤如下: 1. 创建一个QTableView控件和一个QStandardItemModel数据模型,用于显示数据。 ```cpp // 创建QTableView控件和QStandardItemModel数据模型 QTableView *tableView = new QTableView(); QStandardItemModel *model = new QStandardItemModel(); tableView->setModel(model); ``` 2. 创建一个QItemDelegate对象,用于实现单元格的自定义显示和编辑方式。 ```cpp // 创建QItemDelegate对象 QItemDelegate *delegate = new QItemDelegate(); ``` 3. 重写QItemDelegate的createEditor()函数和setEditorData()函数,用于创建和设置单元格编辑器。 ```cpp // 重写createEditor()函数,创建单元格编辑器 QWidget *MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { // 创建一个QComboBox作为单元格编辑器 QComboBox *editor = new QComboBox(parent); editor->addItems(QStringList() << "Item1" << "Item2" << "Item3"); return editor; } // 重写setEditorData()函数,设置单元格编辑器的初始值 void MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { // 获取单元格的值 QString value = index.model()->data(index, Qt::EditRole).toString(); // 将单元格的值设置为QComboBox的当前选项 QComboBox *comboBox = static_cast<QComboBox*>(editor); comboBox->setCurrentText(value); } ``` 4. 将QItemDelegate对象设置到QTableView控件上。 ```cpp // 将QItemDelegate对象设置到QTableView控件上 tableView->setItemDelegate(delegate); ``` 5. 向QStandardItemModel数据模型添加数据,并设置单元格的编辑方式。 ```cpp // 向QStandardItemModel数据模型添加数据 for (int i = 0; i < 5; i++) { for (int j = 0; j < 3; j++) { QStandardItem *item = new QStandardItem(QString("Row %1, Column %2").arg(i).arg(j)); model->setItem(i, j, item); // 设置单元格的编辑方式为自定义的QItemDelegate对象 tableView->setItemDelegateForColumn(j, delegate); } } ``` 这样就可以将combobox、qmodel添加到一个tableview了。其,QStandardItemModel是Qt提供的一个标准数据模型,用于存储和管理数据;QItemDelegate是Qt提供的一个委托类,用于实现单元格的自定义显示和编辑方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值