Swift - 重新排序 UITableView 单元格

标签 swift

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

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

但是在 Swift 中是否有与上述链接等效的东西?

最佳答案

我试过了...这是代码

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

@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 委托(delegate)方法 -->

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

关于Swift - 重新排序 UITableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24772457/

相关文章:

ios - 在 xcode 中启动时模拟器崩溃,问题出在 view.controller.init 中

json - 在 Swift 中动态构建 JSON

ios - 使用 ForEach 时 SegmentedControl 中断

ios - 在执行 unwindSegue 之前检查 UIViewController 是否已初始化

ios - Swift 2 - 如何从 Alamofire.responseJSON 获取数据

swift - 将整个字符串转换为整数并将其快速显示在文本字段中

unit-testing - 为单元测试创​​建模拟云工具包对象

ios - 类型 'UIView' 的值没有成员 'image'

swift - 从 UIDatePicker 获取日期(没有时间)

ios - 像这样的 CollectionView 堆叠单元格如何像钱包一样重叠单元格