Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Reorder UITableView cells

Tags:

swift

I do know that it's not too hard to do it in objective C , the problem is I'm learning Swift by skipping Objective C.

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

However is there anything equivalent to the link above in Swift?

like image 880
Marin Avatar asked Jul 16 '14 04:07

Marin


3 Answers

I have tried this...here is the code

In my example code there is button that starts the editing --- Action Method of the button -->

@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();
    }
}

And the related UITableView delegate methods -->

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

You can also download full code Here

like image 105
Suryakant Sharma Avatar answered Nov 11 '22 16:11

Suryakant Sharma


All the same rules apply as in Objective-C. You set the table view data source and delegate just like you would in 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)
}

If you need finer grain control, you can also implement

func tableView(tableView: UITableView!, targetIndexPathForMoveFromRowAtIndexPath sourceIndexPath: NSIndexPath!, toProposedIndexPath proposedDestinationIndexPath: NSIndexPath!) -> NSIndexPath! {
    …
}
like image 11
Jeffery Thomas Avatar answered Nov 11 '22 15:11

Jeffery Thomas


Now there is a library for this reorder function: LPRTableView.

like image 3
xgdgsc Avatar answered Nov 11 '22 14:11

xgdgsc