自己写的一个tableView自定义索引栏

由于项目美工的强烈要求,根据她的要求写了一个自定义的tableview索引栏,下面是我的代码


/**
         *  数据数组:manager.listArray
         *  索引名称数组:sectionArr
         *  tableView行高:RowHeight
         *  tableView的sectionView高:SectionHeight
         */
        if (iPhone5) {
            indexView = [[IndexView alloc]initWithFrame:CGRectMake(287, 5, 30, 440) WithDataArray:manager.listArray WithNameArray:sectionArr WithRowHeight:50.0 WithSectionHeight:20.0];
        }else{
            indexView = [[IndexView alloc]initWithFrame:CGRectMake(287, 5, 30, 360) WithDataArray:manager.listArray WithNameArray:sectionArr WithRowHeight:50.0 WithSectionHeight:20.0];
        }
        
        indexView.delegate = self;
        [self.view addSubview:indexView];

点h文件

@protocol IndexViewDelegate <NSObject>

@optional
//代理方法
- (void)indexViewChangeToRect:(CGPoint)point;

@end

@interface IndexView : UIImageView
{
    NSMutableArray *indexArr;
    float indexRowHeight;
    float indexSectionHeight;
}

@property(nonatomic,strong)id<IndexViewDelegate>delegate;

- (id)initWithFrame:(CGRect)frame WithDataArray:(NSMutableArray *)arr WithNameArray:(NSMutableArray *)nameArr WithRowHeight:(float)rowHeight WithSectionHeight:(float)sectionHeight;


点m文件

- (id)initWithFrame:(CGRect)frame WithDataArray:(NSMutableArray *)arr WithNameArray:(NSMutableArray *)nameArr WithRowHeight:(float)rowHeight WithSectionHeight:(float)sectionHeight;
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        self.userInteractionEnabled = YES;//开启交互
        
        indexArr = arr;
        indexRowHeight = rowHeight;
        indexSectionHeight = sectionHeight;
        
        //由于需要定义文字大小,所以加入了label
        for (int i = 0 ; i<arr.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.tag = i+2000;
            btn.frame = CGRectMake(0, 0+i*(self.frame.size.height/arr.count), self.frame.size.width, self.frame.size.height/arr.count);
            [btn addTarget:self action:@selector(changeToRect:) forControlEvents:UIControlEventAllTouchEvents];
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, btn.frame.size.width, btn.frame.size.height)];
            label.text = [nameArr objectAtIndex:i];
            label.tag = btn.tag + 1;
            label.font = [UIFont systemFontOfSize:12];
            [label.layer setMasksToBounds:YES];
            label.layer.cornerRadius = 5;
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = RGBCOLOR(80, 80, 80);
            label.backgroundColor = [UIColor clearColor];
            [btn addSubview:label];
            [self addSubview:btn];
        }
        
    }
    return self;
}

static int tagL;
- (void)changeToRect:(UIButton *)btn{
    
    //记录上次的按钮,然后取消背景颜色
    if (tagL!=0) {
        UILabel *label = (UILabel *)[self viewWithTag:tagL];
        label.backgroundColor = [UIColor clearColor];
    }
    
    UILabel *label = (UILabel *)[btn viewWithTag:btn.tag+1];
    tagL = btn.tag+1;
    label.backgroundColor = RGBCOLOR(255, 212, 150);
    
    float num = 0.0;
    
    //计算需要跳转到的位置
    for (int i = 0; i < btn.tag - 2000; i++) {
        num+= ([[indexArr objectAtIndex:i] count]*indexRowHeight) + indexSectionHeight+2;
    }//+2是因为分割线
    
    CGPoint point = CGPointMake(0, num);
    
    if ([self.delegate respondsToSelector:@selector(indexViewChangeToRect:)]) {
        [self.delegate indexViewChangeToRect:point];
    }
    
}

#pragma mark - IndexView的代理方法
//在代理里跳转位置
- (void)indexViewChangeToRect:(CGPoint)point{
    
    [fontListTableView setContentOffset:point animated:YES];

}

这样就ok啦,效果如下:


由于项目需求有限,我这个索引比较简单,之后找到了一个更好的,地址如下:

http://code4app.com/ios/MJNIndexView/522008c56803fafe03000002

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要实现在 QTableView 中使用一个全选的复选框来选择整列的复选框,你可以通过以下步骤来实现: 1. 确保你的模型中的某一列用于显示复选框。你可以使用 QStandardItemModel 或自定义的模型来实现这一点。 2. 创建一个 QCheckBox 作为全选的复选框。 3. 在视图的表头中插入这个全选的复选框。 4. 连接全选复选框的状态改变信号到一个槽函数。 5. 在槽函数中遍历整列,并设置复选框的状态。 以下是一个示例代码,演示如何在 QTableView 中使用一个全选的复选框来选择整列的复选框: ```cpp #include <QCheckBox> #include <QHeaderView> //... // 创建一个 QCheckBox 作为全选的复选框 QCheckBox* selectAllCheckBox = new QCheckBox(); selectAllCheckBox->setTristate(false); // 设置为二态复选框 // 将全选的复选框插入表头视图 tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive); tableView->setViewportMargins(selectAllCheckBox->width(), 0, 0, 0); tableView->horizontalHeader()->viewport()->layout()->addWidget(selectAllCheckBox); // 连接全选复选框的状态改变信号到槽函数 connect(selectAllCheckBox, &QCheckBox::stateChanged, this, &MyClass::onSelectAllCheckBoxStateChanged); // 槽函数:处理全选复选框状态改变事件 void MyClass::onSelectAllCheckBoxStateChanged(int state) { // 遍历整列,设置复选框的状态 for (int row = 0; row < model->rowCount(); ++row) { QModelIndex index = model->index(row, checkboxColumnIndex); // checkboxColumnIndex 表示复选框所在的列索引 model->setData(index, state, Qt::CheckStateRole); } } ``` 在这个例子中,我们首先创建了一个 QCheckBox 作为全选的复选框。然后,我们将全选的复选框插入到表头视图中。为了确保复选框出现在正确的位置,我们设置了适当的视口边距和布局。 最后,我们连接了全选复选框的状态改变信号 `stateChanged` 到槽函数 `onSelectAllCheckBoxStateChanged`。在槽函数中,我们遍历整列,并根据全选复选框的状态设置相应的数据。 注意:在示例代码中,你需要将 `checkboxColumnIndex` 替换为实际的复选框所在列的索引。 希望这个回答对你有所帮助!如果还有其他问题,请随时提问。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值