TableView - 单元格的上移和下移功能

     2023-02-26     237

关键词:

【中文标题】TableView - 单元格的上移和下移功能【英文标题】:TableView - Move up and Move down functionality of a cell 【发布时间】:2015-05-05 09:18:07 【问题描述】:

我有一个TableView 的情况。我正在尝试实现一个允许向上或向下移动单元格的功能。向上或向下移动单元格(带有单元格内容)后,我想将焦点更改为单元格的新位置。

问题是它不会更改到新位置。由于某种原因,它停留在原来选定的单元格位置。

这是用于上移、下移和改变焦点的代码:

我正在尝试移动单个选定的单元格。

public class TableController

    private ObservableList<SimpleStringProperty> observablePrnPropertyData;

    @FXML
    private TableView<SimpleStringProperty> table;
    @FXML
    private TableColumn<SimpleStringProperty, String> data;        

    @FXML
    private void initialize()
    
        this.data.setCellValueFactory(cellData -> cellData.getValue());
        this.data.setCellFactory(event -> new EditCell(this.observablePrnPropertyData, this.table));
    

    public void display(final PrnProperty prnProperty)
    
        this.observablePrnPropertyData = PrnPropertyUtil.getObservableDataFromPrnProperty(prnProperty);
        this.table.setItems(this.observablePrnPropertyData);
    


    private final class EditCell extends TableCell<SimpleStringProperty, String>
    
        @Override
        public void updateItem(String item, boolean empty)
        
            super.updateItem(item, empty);

            if (empty)
            
                this.setText(null);
                this.setGraphic(null);
            
            else
            
                this.setUpContextMenu();

                this.setText(this.getString());
                this.setGraphic(null);
            
        

        private void setUpContextMenu()
        
            // Context menu
            ContextMenu contextMenu = new ContextMenu();

            // context menu Move up
            this.moveUp = new MenuItem("Move up");
            this.moveUp.setOnAction(event -> this.moveUp(this.table, this.observablePrnPropertyData));
            contextMenu.getItems().add(this.moveUp);

            // Context menu for move down
            this.moveDown = new MenuItem("Move down");
            this.moveDown.setOnAction(event -> this.moveDown(this.table, this.observablePrnPropertyData));
            contextMenu.getItems().add(this.moveDown);

            // Add context menu
            this.setContextMenu(contextMenu);
        

        public void moveUp(final TableView<?> table, ObservableList listToManipulate)
        
            final int selectedIndex = table.getSelectionModel().getSelectedIndex();

            final Object removeItem = listToManipulate.remove(selectedIndex);

            final int newIndex = selectedIndex - 1;
            listToManipulate.add(newIndex, removeItem);
            this.changeTableCellFocus(table, newIndex);
        

        public void moveDown(final TableView<?> table, ObservableList listToManipulate)
        
            final int selectedIndex = table.getSelectionModel().getSelectedIndex();

            final Object remove = listToManipulate.remove(selectedIndex);

            final int newIndex = selectedIndex + 1;
            listToManipulate.add(newIndex, remove);
            this.changeTableCellFocus(table, newIndex);
        

        public void changeTableCellFocus(final TableView<?> table, final int focusIndex)
        
            table.requestFocus();
            table.getSelectionModel().clearAndSelect(focusIndex);
            table.getFocusModel().focus(focusIndex);
        
    

如果有人能给出一个可行的例子,那就太好了。我真的很想知道我做错了什么。

【问题讨论】:

您能否提供更多有关如何创建表格的代码。这很重要,因为目前我无法从您的代码中判断您是要移动单个单元格还是整行。此外,数据绑定很重要,因此如果您在其他位置有它,也请添加该代码。 我对代码进行了一些更改以包含更多信息。 您的代码将移动整行,而不仅仅是一个单元格(尽管现在看起来您只有一列)。这是一个完整的 hack,但请尝试将对 changeTableCellFocus(...) 的调用封装在 Platform.runLater(...) 中。 我已经尝试过Platform.runLater(...),但我仍然遇到同样的问题。整行向上移动,但焦点不会更改到新行位置。 javafx-1 "= javafx script is notlevant" 在这里和所有其他 javafx 问题中! 【参考方案1】:

对于您的情况,使用此代码可以正常工作,很简单,您必须首先获取所选索引,将项目移动到下一个(或上一个)行,然后选择这个新行。

void upClicked()
    if(table.getSelectionModel().getSelectedItem() != null) // check if the user really selected a row in the table
    
        if(table.getSelectionModel().getSelectedIndex() != 0) // if the row first one so do nothing
        
            int index = table.getSelectionModel().getSelectedIndex(); // get the selected row index
            SimpleStringProperty x = table.getSelectionModel().getSelectedItem(); // get the selected item
            table.getItems().set(index, table.getItems().get(index-1)); // move the selected item up
            table.getItems().set(index-1, x); // change the row with the item in above
            table.getSelectionModel().select(index-1); // select the new row position
        
    


void downClicked()
    if(table.getSelectionModel().getSelectedItem() != null)
    
        if(table.getSelectionModel().getSelectedIndex() != table.getItems().size()-1) // if the row is in last so dont do nothing
        
            int index = table.getSelectionModel().getSelectedIndex();
            SimpleStringProperty x = table.getSelectionModel().getSelectedItem();
            table.getItems().set(index, table.getItems().get(index+1));
            table.getItems().set(index+1, x);
            table.getSelectionModel().select(index+1);
        
    

【讨论】:

【参考方案2】:

如果您只需要关注下一行和上一行,您可以尝试:table.getFocusModel().focusNext() 和 .focusPrevious(),如下所述:http://docs.oracle.com/javafx/2/api/javafx/scene/control/FocusModel.html

【讨论】:

我已经尝试过了,但我仍然遇到同样的问题。我一定做错了什么,但不确定是什么。如果有人能给出一个可行的例子,那就太好了。

jqgrid上移下移单元格(代码片段)

在表格中常常需要调整表格中数据的显示顺序,我用的是jqgrid,实现原理就是将表中的行数保存到数据库中,取数据时按行进行排序1、上移,下移按钮<ahref="javascript:void(0)"onclick="operateWithOneRowById(up)"class="linkButton">上移</a>... 查看详情

vue做一个上移和下移,删除的li功能(代码片段)

效果图:思路就是冒泡原理,把数据放到一个空数组,对其进行排序,单选框用到的是iview。具体实现代码:<divv-for="iteminsingledLists":key="item.index">//数组singledLists<Checkbox@on-change="checkSingle":disabled="isDisabled"v-model="item.isRight 查看详情

tableView 单元格的正常和选定状态的不同象形图

】tableView单元格的正常和选定状态的不同象形图【英文标题】:DifferentpictogramsfornormalandselectedstateoftableViewcells【发布时间】:2012-10-0714:23:37【问题描述】:这是我的设置。带有自定义设计单元格的表格视图,可重复使用多次。布... 查看详情

tableview 单元格我们如何快速调整单元格的大小以及图像和标签

】tableview单元格我们如何快速调整单元格的大小以及图像和标签【英文标题】:tableviewcellhowdoweresizecellinswiftalongwithimageandlabel【发布时间】:2016-10-0605:55:35【问题描述】:当我们在tableview单元格中有一个标签和图像时,我们如何... 查看详情

lind.ddd.domain.isortbehavor~上移与下移

在进行列表排序时,有个“上移”和“下移”操作,这个一般在内存里完成,然后统一提交到数据库中,对于上移与下移的设计,大叔在LIND.DDD.DOMAIN里有一个ISortBehavor接口,主要是说,如果实体对象支持排序功能,... 查看详情

使用 NSMutableDictionary 和数组填充单元格的 TableView

】使用NSMutableDictionary和数组填充单元格的TableView【英文标题】:TableViewwithNSMutableDictionaryandarraystofillinthecells【发布时间】:2012-02-0806:32:21【问题描述】:我刚开始学习Xcode,阅读了这本书,现在尝试实现我阅读的内容。该应用程... 查看详情

使标签等于 TableView 单元格的标题和副标题

】使标签等于TableView单元格的标题和副标题【英文标题】:MakeLabelsequalTableViewCell\'sTitleandSubtitle【发布时间】:2017-01-0514:09:21【问题描述】:所以我只是从编码开始。我有一个表格视图,其中通过代码将单元格添加到其中。我有... 查看详情

带有 Tableview 和静态单元格的空弹出框

】带有Tableview和静态单元格的空弹出框【英文标题】:EmptyPopoverwithTableviewandStaticCells【发布时间】:2013-01-1518:37:15【问题描述】:我有一个表格视图设置,其中包含一些在弹出窗口中显示的静态单元格。问题是弹出框总是空的,... 查看详情

iOS:访问下一个 Tableview 单元格的按钮

】iOS:访问下一个Tableview单元格的按钮【英文标题】:iOS:ButtontoaccessnextTableviewcell【发布时间】:2011-07-2116:33:57【问题描述】:我有一个主视图控制器,它是一个表视图,从表视图中选择一个单元格后,会显示另一个视图。如何... 查看详情

自定义 Tableview 单元格的问题

】自定义Tableview单元格的问题【英文标题】:TroublewithcustomTableviewcell【发布时间】:2011-09-2111:12:29【问题描述】:我的应用由3个视图组成(视图1(Tableview1)、视图2(Tableview2)、视图3(DetailView))。我在我的视图2(Tableview2)中设置了一... 查看详情

元素的上移下移等排序操作

...项目中,我经常遇到对元素进行排序操作的需求,包括:上移、下移、置顶、置底。那么这些操作如何实现呢? 上移,简言之就是将需要上移的元素和它前面元素交换位置,使用insertBefore(),大致思路为:vardom=需要上移的元... 查看详情

在 iOS 13 和 xcode 11 中,tableview 单元格的“setSelected”方法不返回完美的单元格大小

】在iOS13和xcode11中,tableview单元格的“setSelected”方法不返回完美的单元格大小【英文标题】:"setSelected"methodoftableviewcellnotreturnperfectsizeofcelliniOS13andxcode11【发布时间】:2019-10-0810:46:26【问题描述】:实际上,我有一个在t... 查看详情

如何在 UITableView 的 RXswift 和 RXCocoa 中实现 tableview 单元格的内部?

】如何在UITableView的RXswift和RXCocoa中实现tableview单元格的内部?【英文标题】:HowtoimplementinRXswiftandRXCocoaofUITableViewInsideoftableviewcell?【发布时间】:2018-08-2114:13:17【问题描述】:我是RXswift新手,目前正在从事一个需要rxswift和UITable... 查看详情

如何以编程方式将约束添加到 tableView 单元格的默认 textLabel 和 accessoryView

】如何以编程方式将约束添加到tableView单元格的默认textLabel和accessoryView【英文标题】:HowtoAddconstraintsprogrammaticallytodefaulttextLabelandaccessoryViewoftableViewcell【发布时间】:2017-12-2209:40:06【问题描述】:我有一个单元格,因为我想要... 查看详情

如何删除不希望保留超级视图继承边距的 tableView 单元格的顶部和左侧边距?

】如何删除不希望保留超级视图继承边距的tableView单元格的顶部和左侧边距?【英文标题】:HowtoremovethetopandleftmarginofatableView\'scellthatdoesn\'texpecttopreservethesuperview\'sinheritedmargins?【发布时间】:2016-06-2110:58:00【问题描述】:最初... 查看详情

使用 NSCoding 保存 TableView 单元格的重新排序

】使用NSCoding保存TableView单元格的重新排序【英文标题】:SavingreorderofTableViewcellsusingNSCoding【发布时间】:2016-02-0119:08:59【问题描述】:我正在使用Xcode7和Swift2处理一个iOS项目。我有一个TableView,它从NSCoding获取array。我已经启动... 查看详情

计算tableview中每个单元格的值(代码片段)

我正在尝试从每个tableViews单元格中获取总数,然后添加到总标签。由于每个单元格可能有不同的数量和价格,我使用Arrays数量和产品基价。我已经按照这个问题/答案,但看着问的人正在使用结构:howtocalculatethevaluesintableviewandtod... 查看详情

Swift:使用 tableView DidSelect 方法更改单元格的 UIButton 图像

】Swift:使用tableViewDidSelect方法更改单元格的UIButton图像【英文标题】:Swift: Changethecell\'sUIButtonImageWithtableViewDidSelectmethod【发布时间】:2019-01-2205:20:13【问题描述】:我有TableView,在Tableview中有两个标签和一个UIButton。所以在DidS... 查看详情