Swift-对UITableView单元格重新排序

Swift-对UITableView单元格重新排序,swift,Swift,我知道在目标C中做并不难,问题是我正在跳过目标C学习Swift 但是,Swift中是否有与上述链接等效的内容?所有规则都适用于Objective-C。您可以像在Objective-C中一样设置表视图数据源和委托 func tableView(tableView: UITableView!, canMoveRowAtIndexPath indexPath: NSIndexPath!) -> Bool { return true // Yes, the table view can

我知道在目标C中做并不难,问题是我正在跳过目标C学习Swift


但是,Swift中是否有与上述链接等效的内容?

所有规则都适用于Objective-C。您可以像在Objective-C中一样设置表视图数据源和委托

func tableView(tableView: UITableView!, canMoveRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
    return true // Yes, the table view can be reordered
}

func tableView(tableView: UITableView!, moveRowAtIndexPath fromIndexPath: NSIndexPath!, toIndexPath: NSIndexPath!) {
    // update the item in my data source by first removing at the from index, then inserting at the to index.
    let item = items[fromIndexPath.row]
    items.removeAtIndex(fromIndexPath.row)
    items.insert(item, atIndex: toIndexPath.row)
}
如果需要更精细的粒度控制,还可以实现

func tableView(tableView: UITableView!, targetIndexPathForMoveFromRowAtIndexPath sourceIndexPath: NSIndexPath!, toProposedIndexPath proposedDestinationIndexPath: NSIndexPath!) -> NSIndexPath! {
    …
}

我已经试过了…这是密码

在我的示例代码中,有一个开始编辑的按钮--- 按钮的操作方法-->

以及相关的UITableView委托方法-->


您还可以下载完整的代码,现在有一个用于此重新排序功能的库:。

在Swift 3.0中转换了上述应答方法

    // Determine whether a given row is eligible for reordering or not.
    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
            return true
    }


    // Process the row move. This means updating the data model to correct the item indices.
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        let item : Dictionary<String, Any> = arrInterval[sourceIndexPath.row]
        arrInterval.remove(at: sourceIndexPath.row)
        arrInterval.insert(item, at: destinationIndexPath.row)
    }
//确定给定行是否符合重新排序的条件。
func tableView(tableView:UITableView,canMoveRowAt indexath:indexPath)->Bool{
返回真值
}
//处理行移动。这意味着更新数据模型以更正项目索引。
func tableView(tableView:UITableView,moveRowAt sourceindexath:indexath,to destinationindexath:indexath){
let项:Dictionary=arrInterval[sourceIndexPath.row]
arrInterval.remove(位于:sourceIndexPath.row)
插入(项,位于:destinationIndepath.row)
}

您可以从Swift调用Objective-C。您好,zneak,就像pure Objective-C不需要包装一样?是的,pure Objective-C不需要包装。我需要更新tableView还是拥有此aTableView。deleteRowsAtIndexPaths([fromIndexPath.row],with RowAnimation:.Fade)@Marin当
tableView时,表视图已经知道更新(,moveRowatineXpath,toIndexPath)
被调用。您所需要做的就是更新数据源以匹配表视图中的更新。代码可以工作,但什么也没有发生……出于某种原因。我的一个问题是,我有一个按钮,它必须是barButtonItem吗?在这种情况下,您必须在某些地方(如UIBarButton)更改为UIButtonGreat谢谢,我想知道是否有我如何更改UIButton的文本?当然,您可以通过
setTitle(title:String?,forState:)
更改UIButton标题。如果(false==self.editing&&!indexPath);//==未找到重载,请提供一个更新的UIButton标题。源代码中也有7个错误,请提供一个更新的源代码,感谢您的努力
// The editing style for a row is the kind of button displayed to the left of the cell when in editing mode.

        func tableView(tableView: UITableView!, editingStyleForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCellEditingStyle
        {
            if (false == self.editing && !indexPath){
                return UITableViewCellEditingStyle.None;
            }

            if (self.editing && indexPath.row == countryList.count){
                return UITableViewCellEditingStyle.Insert;
            }
            else{
                return UITableViewCellEditingStyle.Delete;
            }
            //return UITableViewCellEditingStyle.Delete;
        }

        // Update the data model according to edit actions delete or insert.
        func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!)
        {
            if editingStyle == UITableViewCellEditingStyle.Delete{
                countryList.removeAtIndex(indexPath.row);
                self.editTableView(barButton);
                listTableView.reloadData();
            }
            else if editingStyle == UITableViewCellEditingStyle.Insert{
                countryList.append("New Country");
            }
        }


        // Determine whether a given row is eligible for reordering or not.
       func tableView(tableView: UITableView!, canMoveRowAtIndexPath indexPath: NSIndexPath!) -> Bool
        {
            return true;
        }

        // Process the row move. This means updating the data model to correct the item indices.
        func tableView(tableView: UITableView!, moveRowAtIndexPath sourceIndexPath: NSIndexPath!, toIndexPath destinationIndexPath: NSIndexPath!)
        {
            let item : String = countryList[sourceIndexPath.row];
            countryList.removeAtIndex(sourceIndexPath.row);
            countryList.insert(item, atIndex: destinationIndexPath.row)
        }
    // Determine whether a given row is eligible for reordering or not.
    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
            return true
    }


    // Process the row move. This means updating the data model to correct the item indices.
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        let item : Dictionary<String, Any> = arrInterval[sourceIndexPath.row]
        arrInterval.remove(at: sourceIndexPath.row)
        arrInterval.insert(item, at: destinationIndexPath.row)
    }