简单tableView的使用

UITableView是一个用于显示列表的视图,可以作为子视图镶嵌在主视图上,可以滑动,选取各种参数

定义:

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{


@property (nonatomic, retain) NSArray *dataList;

@property (nonatomic, retain) UITableView *myTableView;


    UITableView *tableView = [[UITableViewalloc] initWithFrame:FRAME_TABLEVIEWstyle:UITableViewStylePlain];

注意:这里的两个属性 dataList和myTableView并不要求实现,只需要在头文件定义就可以了

Delegate必须要有,因为必须要实现它的接口

使用:

    //init tableView

    NSArray *list = [NSArray arrayWithObjects:@"模糊",@"素描",@"怀旧", nil];

    self.dataList = list;

    // 设置tableView的数据源

    tableView.dataSource = self;

    // 设置tableView的委托

    tableView.delegate = self;

    // 设置tableView的背景图

//    tableView.backgroundView = [[UIImageView alloc] init];

    self.myTableView = tableView;

    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self.view addSubview:self.myTableView];

常用的属性设置:

    //设置转置,使table view倒过来

    tableView.transform = CGAffineTransformMakeRotation(-M_PI / 2);

    //取消滑动出现删除

    tableView.showsVerticalScrollIndicator = NO;

接口的实现:

 

//set cells

//定义每个列表项的属性

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CellWithIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellWithIdentifier];

    }

    cell.imageView.image = [UIImageimageNamed:@"example.png"];

    return cell;

}

 

//set number of sections

//定义块的个数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}

 

//行首缩进

//定义首行缩进的宽度

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 0;

}

 

//row height

//定义每一行的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 105;

}

 

 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

//    if ([indexPath row] % 2 == 0) {

//        cell.backgroundColor = [UIColor blueColor];

//    } else {

//        cell.backgroundColor = [UIColor greenColor];

//    }

}

 

 

//select rows callback

//选择某行以后的动作方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    switch ([indexPath row]) {

        case 0:

            testFilter = [[GPUImageFastBlurFilteralloc] init];

            break;

        case 1:

            testFilter = [[GPUImageSketchFilteralloc] init];

            break;

        case 2:

            testFilter = [[GPUImageSepiaFilteralloc] init];

            break;

        default:

            break;

    } 

}

 

 

// delete

//选择删除以后的方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"执行删除操作");

}

 

//设置没有分界线

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    returnUITableViewCellEditingStyleNone;

}

 

 

//number of rows

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    returnself.dataList.count;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt TableView 是一个用于显示和编辑表格数据的控件。它是基于模型-视图框架实现的,可以与各种数据模型进行交互。下面是一个简单的示例,演示如何使用 Qt TableView: 1. 首先,在你的 Qt 项目中包含 `QTableView` 和相关的头文件: ```cpp #include <QTableView> #include <QStandardItemModel> ``` 2. 创建一个 `QTableView` 对象和一个数据模型 `QStandardItemModel`: ```cpp QTableView *tableView = new QTableView(this); QStandardItemModel *model = new QStandardItemModel(this); ``` 3. 设置数据模型的行数和列数: ```cpp model->setRowCount(4); model->setColumnCount(3); ``` 4. 可以设置表头信息,如果需要的话: ```cpp model->setHorizontalHeaderLabels({"Name", "Age", "Country"}); ``` 5. 设置表格数据: ```cpp model->setItem(0, 0, new QStandardItem("John")); model->setItem(0, 1, new QStandardItem("25")); model->setItem(0, 2, new QStandardItem("USA")); // 可以通过循环来设置更多的数据项 ``` 6. 将数据模型设置给 TableView: ```cpp tableView->setModel(model); ``` 7. 可以根据需要调整表格的大小和行列大小: ```cpp tableView->resizeColumnsToContents(); tableView->resizeRowsToContents(); ``` 8. 最后,将 TableView 添加到你的窗口或布局中: ```cpp layout->addWidget(tableView); ``` 这样就完成了一个简单TableView使用。你可以根据实际需求进一步自定义表格的样式和行为。希望这能帮到你!如果你有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值