多个TableView

由于tableview需要有datasource和delegate,因此,你在程序里写的时候,如果多个tableview则用同一个方法使用。因此,去判定的话,需要这样去分辨
  1. #pragma mark -UITableViewDataSource
  2. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  3.    
  4.     if (tableView == recircleView){
  5.         //如果当前的表是回收的
  6.         return [list_recircle count];
  7.     }
  8.     if (tableView == useView){
  9.         //如果当前的表是使用的
  10.         return [list_use count];
  11.     }
  12.     return 0;
  13. }

  14. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  15.     static NSString *UseTerminalCell = @"UseTerminalCell";
  16.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:UseTerminalCell];
  17.     if (cell == nil) {
  18.         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:UseTerminalCell];
  19.     }
  20.    
  21.     if (tableView == recircleView) {
  22.         if(list_recircle != nil && [list_recircle count]>0){
  23.             cell.textLabel.text =[ list_recircle objectAtIndex: [indexPath row] ];//写单元格的值
  24.         }
  25.     }else if (tableView == useView) {
  26.          cell.textLabel.text =[ list_use objectAtIndex: [indexPath row] ];//写单元格的值
  27.     }
  28.   
  29.     cell.textLabel.font = [UIFont systemFontOfSize:13];//设置字体
  30.     return cell;
  31. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于Qt的TableView,通常我们不推荐直接对TableView进行多继承自定义类的操作。相反,我们可以通过使用组合或者委托来自定义TableView的行为。 要自定义TableView的行为,您可以采用以下方法之一: 1. 组合:创建一个新的类,将TableView作为成员变量,并在新类中添加需要的功能或者重写需要修改的方法。 ```cpp class MyTableView : public QWidget { Q_OBJECT public: explicit MyTableView(QWidget *parent = nullptr) : QWidget(parent) { tableView_ = new QTableView(this); // 添加需要的功能或重写方法 } private: QTableView *tableView_; }; ``` 2. 委托:创建一个自定义委托类,然后将其应用到TableView的特定列中。这样可以在显示和编辑数据时自定义单元格的行为。 ```cpp class MyDelegate : public QItemDelegate { Q_OBJECT public: explicit MyDelegate(QObject *parent = nullptr) : QItemDelegate(parent) {} QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override { // 自定义编辑器 // ... } void setEditorData(QWidget *editor, const QModelIndex &index) const override { // 设置编辑器的数据 // ... } void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override { // 获取编辑器的数据并更新模型 // ... } void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override { // 更新编辑器的位置和大小 // ... } }; ``` 然后将自定义委托应用到TableView的特定列: ```cpp QTableView *tableView = new QTableView(this); MyDelegate *delegate = new MyDelegate(this); tableView->setItemDelegateForColumn(columnIndex, delegate); ``` 通过组合或者委托的方式,您可以灵活地自定义TableView的行为,而无需多继承自定义类。希望对您有所帮助!如果您还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值