Swift - 重新排序UITableView单元格

时间:2014-07-16 04:52:49

标签: swift

我知道在目标C中做到这一点并不太难,问题是我通过跳过Objective C来学习Swift。

https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/ManageReorderRow/ManageReorderRow.html

然而,有什么东西等同于Swift上面的链接吗?

4 个答案:

答案 0 :(得分:10)

我试过这个......这是代码

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

@IBAction func editTableView (sender:UIBarButtonItem)
{
    if listTableView.editing{
        //listTableView.editing = false;
        listTableView.setEditing(false, animated: false);
        barButton.style = UIBarButtonItemStyle.Plain;
        barButton.title = "Edit";
        //listTableView.reloadData();
    }
    else{
        //listTableView.editing = true;
        listTableView.setEditing(true, animated: true);
        barButton.title = "Done";
        barButton.style =  UIBarButtonItemStyle.Done;
        //listTableView.reloadData();
    }
}

相关的UITableView委托方法 - >

// 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)
        }

您还可以下载完整代码 Here

答案 1 :(得分:9)

所有相同的规则都适用于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! {
    …
}

答案 2 :(得分:3)

现在有一个用于此重新排序功能的库:LPRTableView

答案 3 :(得分:0)

在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)
    }