tableView-思路简介

本文介绍了UITableView如何通过设置数据源来显示数据,包括数据源的设置步骤、cell的复用机制、数据刷新方法以及编辑模式下的插入和删除操作。

* tableView继承自scrollView

 

我们需要设置数据源才能显示数据,设置数据源和代理很类似,三步(

1.遵循数据源协议

2.设置数据源

3.实现数据源方法

头部和尾部标题
(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

* cel的accessoryView属性必须要设置大小

 

cell的的backgroundView以及selectBackgroundView

 

设置cell的分割线距离上左下右的距离,一般上和下边没有效果

  cell.separatorInset = UIEdgeInsetsMake(20, 0, 30, 40);

 

*tableView的属性:分割线的样式 (seperatestyle)

 


1.带着一个重用的标示符去缓存池中取重用cell

static NSString *ID = @"hero";
UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:ID];
2.如果没有重用的cell,我们创建一个 cell
if (cell == nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    
}

3.给cell设置数据覆盖原来的数据


HeroModel *hero = self.heros[indexPath.row];


//      3.1设置名称


cell.textLabel.text = hero.name;


////     3.2设置图片

cell.imageView.image = [UIImage imageNamed: hero.icon];

////     3.3设置子标题

cell.detailTextLabel.text = hero.intro;


////     3.4设置指示器

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

 

刷新数据(两个步骤)
1>修改模型
2>刷新数据(两种方式:一种全局刷新,一种局部刷新,局部刷新性能较高)
[self.tableView reloadData];//全局刷新
2.1局部刷新
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]

 

 

 


7.删除插入cell(总体原则,修改模型,刷新数据)(实现三个方法)

/**
 *  表示tableView能够进行编辑
 *
 */
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
    
}
/**
 *  表示编辑模式,当我们想要进行cell的插入和删除操作的时候就会调用这个方法
 *
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle ==  UITableViewCellEditingStyleDelete ) {
        //        1.修改模型(删除模型)
        HeroModel *hero = self.heros[indexPath.row];
        [self.heros removeObject:hero];
        //        2.刷新数据
        
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        
      
    }else if (editingStyle == UITableViewCellEditingStyleInsert){
        
        //        1.修改模型(插入模型)
        HeroModel *hero = [[HeroModel alloc]init];
        hero.name = @"大家都很精神";
        [self.heros insertObject:hero atIndex:indexPath.row];
        //        2.刷新
        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        
    }
   
}
/**
 *  表示修改编辑模式
 *
 */

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleInsert;
    
}

 

copy, paste cell中的数据(三个方法必须实现)

/**
 *  表示能够让弹出粘贴面板
 *
 */
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}
//表示能够执行复制粘贴剪切的方法
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    
    return YES;
}

/**
 *  /
 *
 表示执行action(复制,粘贴,拷贝)方法
 
 */
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    
    //     1.去得模型
    HeroModel *hero = self.heros[indexPath.row];
    //     1.1给粘贴面板赋值
    
    if (action == @selector(copy:)) {
        [UIPasteboard generalPasteboard].string = hero.icon;
    }
    if (action == @selector(paste:)) {
        
        
        hero.icon = [UIPasteboard generalPasteboard].string;
        
        
        [self.tableView reloadData];
    }  
    
}

 

转载于:https://www.cnblogs.com/gp886/p/4941012.html

#include <QApplication> #include <QMainWindow> #include <QTableView> #include <QStandardItemModel> #include <QStandardItem> #include <QHeaderView> #include <QHBoxLayout> #include <QWidget> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建主窗口 QMainWindow mainWindow; QWidget *centralWidget = new QWidget(&mainWindow); QHBoxLayout *layout = new QHBoxLayout(centralWidget); // 创建模型 (22行 = 20数据行 + 2表头行,15列) QStandardItemModel *model = new QStandardItemModel(22, 15, centralWidget); // 设置二级表头数据 // 第一行表头(组合标题) model->setItem(0, 7, new QStandardItem("第一组")); model->setItem(0, 10, new QStandardItem("第二组")); // 第二行表头(各列标题) for (int col = 0; col < 15; ++col) { QString header = QString("列%1").arg(col + 1); model->setItem(1, col, new QStandardItem(header)); } // 设置数据行内容 for (int row = 2; row < 22; ++row) { for (int col = 0; col < 15; ++col) { QStandardItem *item = new QStandardItem( QString("R%1C%2").arg(row-1).arg(col+1) ); model->setItem(row, col, item); } } // 创建表格视图 QTableView *tableView = new QTableView(); tableView->setModel(model); // 隐藏默认表头 tableView->horizontalHeader()->setVisible(false); tableView->verticalHeader()->setVisible(false); // 合并表头单元格 tableView->setSpan(0, 7, 1, 3); // 第一组 (8-10列) tableView->setSpan(0, 10, 1, 3); // 第二组 (11-13列) // 设置统一的行高(25像素) const int rowHeight = 25; for (int row = 0; row < 22; ++row) { tableView->setRowHeight(row, rowHeight); } // 设置表头样式 for (int col = 0; col < 15; ++col) { if (model->item(0, col)) { model->item(0, col)->setTextAlignment(Qt::AlignCenter); model->item(0, col)->setBackground(QBrush(QColor(200, 220, 255))); } if (model->item(1, col)) { model->item(1, col)->setTextAlignment(Qt::AlignCenter); model->item(1, col)->setBackground(QBrush(QColor(230, 240, 255))); } } // 设置固定大小 tableView->setFixedSize(900, 550); // 设置列宽 for (int col = 0; col < 15; ++col) { tableView->setColumnWidth(col, 60); } // 添加到布局 layout->addWidget(tableView); centralWidget->setLayout(layout); mainWindow.setCentralWidget(centralWidget); mainWindow.setWindowTitle("统一行高的多级表头表格"); mainWindow.resize(920, 600); mainWindow.show(); return app.exec(); } 上面的程序的基础上,0-7,14-15没有两级表头的列,不用分开为两行,合并为一行就行,给完整出程序与效果图
最新发布
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值