用 TableView 的代理方法添加的header点击第一个不触发事件,但是点击第二个header触发了第二组第一行的点击事件

在使用TableView代理方法添加header时,遇到一个问题:点击第一个header无响应,而点击第二个及以后的header会触发相应组的第一个cell的点击事件。代码中设置了header背景色为绿色,并根据section设置不同的文字。寻求解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接上添加 header 的代码吧:

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{
        let cellID = "headerCell"
        var headerCell = tableView.dequeueReusableCellWithIdentifier(cellID)
            as? HeaderCell
        headerCell?.backgroundColor = UIColor.greenColor()
        if(headerCell == nil){
            headerCell = HeaderCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellID)
        }
        
        switch section {
        case 0:
            headerCell!.headerLabel.text = "模式"
            break
        case 1:
            headerCell!.headerLabel.text = "风速"
            break
        default:
            headerCell!.headerLabel.text = "other"
            break
        }
        
        return headerCell!
    }


如图,绿色部分为header,点击第一个不会触发cell的事件,但是点击第二个和之后的header会触发当前组第一个cell的点击事件,望大神们看看到底哪里出了问题,谢谢(实在是没分数了,仅有20分,请包涵)
class TableModel(QAbstractTableModel): def __init__(self, data=None, headers=None, parent=None): super().__init__(parent) # 初始化数据(二维列表:每行是一个列表,对应表格的一行数据) #self._data = data if data else [] self._data = [] # 列标题(列表,如["姓名", "年龄", "性别"]) #self._headers = headers if headers else [] self._headers = ["SID", "Time", "Chn", "T/Rx", "ID_index", " SendType", "RemoteFlag", "ExternFlag", "Len", "Data(hex)", "button"] def rowCount(self, parent=QModelIndex()): #"""返回行数(必须重写)""" return len(self._data) def columnCount(self, parent=QModelIndex()): #"""返回列数(必须重写)""" # new 0722 # return len(self._headers) if self._data else 0 return len(self._headers) # new 0722 # 单元格数据 def data(self, index, role=Qt.DisplayRole): #"""返回指定位置的数据(必须重写)""" # new 0723 if role == Qt.TextAlignmentRole: return Qt.AlignCenter # new 0723 if not index.isValid() or role != Qt.DisplayRole: return None # 仅处理显示角色(DisplayRole) row = index.row() col = index.column() # new 0723 if role == Qt.DisplayRole and col == 0: value = self._data[row][col].split(" ")[0] return str(value) # new 0723 if row >= len(self._data) or col >= len(self._headers): return None return self._data[row][col] # 返回第row行第col列的数据 # 表头 def headerData(self, section, orientation, role=Qt.DisplayRole): #"""返回列标题(可选重写)""" if orientation == Qt.Horizontal and role == Qt.DisplayRole: return self._headers[section] if section < len(self._headers) else None # new 添加行号行头 0721 if orientation == Qt.Vertical and role == Qt.DisplayRole: return str(section + 1) # new 添加行号行头 0721 return None # 垂直标题(行号)默认使用行索引,无需额外处理 def add_row(self, row_data): #"""动态添加一行数据(触发视图更新)""" # 通知视图:开始插入行(参数:父索引、插入位置起始行、结束行) self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount()) self._data.append(row_data) # 实际添加数据到模型 self.endInsertRows() # 通知视图:插入完成 怎么修改tableview属性为可编辑的
07-26
def add_table_row(mainwindow, chn, trx, id, SendType, RemoteFlag, ExternFlag, length, data_hex, SID = -1, subFlag = "", time="-1", list_index = 0, CF_flag = 0): # print("CF_flag1:", CF_flag) print("list_index:", list_index) # #SID是10进制数字 #id是0X开头的16进制字符串 #获取新行的位置,table的行下标从0开始 if len(mainwindow.FilterIDList) == 0 or ( int(id, 16) in mainwindow.FilterIDList): time = get_now_time() #向data_hex后填充0,凑齐23个字符 # 将字符串分割成每两个字符一组 hex_list = data_hex.split() if len(hex_list[-1]) < 2: hex_list[-1] += "0" # 计算需要补充的组数 num_groups_to_add = 8 - len(hex_list) # 补充0到列表中 hex_list.extend(['00'] * num_groups_to_add) # 将列表重新组合成字符串,每组之间有一个空格 formatted_hex_str = ' '.join(hex_list) SID_str = hex(SID).upper()[2:] # #构造新行的内容 # items = [SID_str + " " + subFlag + CF_flag_str, time, chn, trx, id + " " + str(list_index), SendType, RemoteFlag, ExternFlag, length, formatted_hex_str] # new 0722 items = [SID_str + " " + subFlag, time, chn, trx, id + " " + str(list_index), SendType, RemoteFlag, ExternFlag, length, formatted_hex_str, ""] print("CF_flag2:", CF_flag) # CF_flag = 1 if CF_flag == 0: print("yes") print("CF_flag3:", CF_flag) mainwindow.right_layout_object.table_view.setItemDelegateForColumn(10, ButtonDelegate()) # 调用模型添加行 mainwindow.right_layout_object.model.add_row(items) mainwindow.right_layout_object.table_view.scrollToBottom() 通过该函数传入CF_flag的值(一般为0或1) 使用Pyside6 tableview来逐行添加按钮并判断CF_flag的值来决定是否添加按钮 class ButtonDelegate(QStyledItemDelegate): def paint(self, painter, option, index): if index.column() == 10: button_option = QStyleOptionButton() button_option.rect = option.rect.adjusted(4, 4, -4, -4) button_option.text = "解析" button_option.state = QStyle.StateFlag.State_Enabled QApplication.style().drawControl(QStyle.ControlElement.CE_PushButton, button_option, painter) else: super().paint(painter, option, index) 在此代码基础上进行修改
最新发布
07-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值