只需单击即可编辑表格单元格,我希望仅通过双击即可编辑它。只需单击即可选择单元格。
我正在使用 uitable
的这个属性:
set(hTable, 'Data',data,...
'ColumnEditable', edit,...
最佳答案
首先,您需要将单元格可编辑性默认设置为 false:
set(hTable,'ColumnEditable', [false false ...]); %accordingly your number of columns
并引入一个CellSelectionCallback
:
set(hTable,'CellSelectionCallback',@cellSelect);
在同一脚本中调用以下函数
function cellSelect(src,evt)
getstate = get(src,'ColumnEditable'); %gets status of editability
index = evt.Indices; %index of clicked cell
state = [false false ...]; %set all cells to default: not editable
state(index) = ~getstate(index); %except the clicked one, was it
%already false before set it true
set(src,'ColumnEditable', state) %pass property to table
end
还有一个CellEditCallback
:
set(hTable,'CellEditCallback',@cellEdit);
调用
function cellEdit(src,~)
state = [false false ...];
set(src,'ColumnEditable', state)
end
<小时/>
最小示例
function minimalTable
h = figure('Position',[600 400 402 100],'numbertitle','off','MenuBar','none');
defaultData = {'insert number...' , 'insert number...'};
uitable(h,'Units','normalized','Position',[0 0 1 1],...
'Data', defaultData,...
'ColumnName', [],'RowName',[],...
'ColumnWidth', {200 200},...
'ColumnEditable', [false false],...
'ColumnFormat', {'numeric' , 'numeric'},...
'CellSelectionCallback',@cellSelect);
end
function cellSelect(src,evt)
getstate = get(src,'ColumnEditable');
index = evt.Indices;
state = [false false];
state(index) = ~getstate(index);
set(src,'ColumnEditable', state)
end
function cellEdit(src,~)
state = [false false];
set(src,'ColumnEditable', state)
end
<小时/>
正如您所发现的,这并不总是有效。因为您遇到的问题与我之前使用弹出菜单时遇到的问题相同。这是完全相同的问题:ColumnEditable 只是一个行向量而不是矩阵。我必须处理 ColumnFormat 属性,它也只是一个行向量。如果双击功能对您确实很重要,您可以引用以下两个答案:
How to deselect cells in uitable / how to disable cell selection highlighting?
这些线程基本上建议为每一行创建一个唯一的 uitable
,以便每一行都有一个唯一的 ColumnEditable
属性。这是迄今为止唯一的解决方案。
恐怕没有简单的解决方案。除了其他答案的复杂解决方法之外,我无法为您提供进一步的帮助。或者只使用上面的简单方法并接受一些小缺点。
关于matlab - 表格单元格只能通过双击进行编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19789747/