SwiftUI List: Add move, delete, update feature, introducing ‘HANDY’ iOS API to handle list.

iOS Handy API to handle list

Today, I will show you the easiest way to implement add, edit, remove, move functionality. Apple has prepared those handy API to use!

[1] Set Up UI

Basic UI

I have tasks Array to save tasks and task for the textfield. The important thing is that you should wrap your entire view code inside NavigationView.

[2] Add

Button("Add Task") {  tasks.append(taskName)  taskName = ""}

Whenever user clicks ‘Add Task’ button, now it will append ‘task’ to ‘tasks’ array.

[3] Delete

Here comes Apple’s pre-built functionality.

This is why you use ForEach

Do you see the comment “Why Using ForEach?”. It’s because ForEach has some useful functionalities like onDelete and onMove.

There’s also a much better way.

Concise Closure Syntax

To use this syntax, you must know that the function you define must have the same parameter name and datatype. If you define function like deleteTask(index: IndexPath) this code won’t work.

[4] MOVE

move task

By calling onMove function, you can reposition your cell.

[5] Edit

Remember I told you to embed your view in NavigationView? This is why.

Edit Button will be added in trailing position

When you apply .toolbar modifier and create EditButton(). Edit Button in navigationbar’s trailing position will show. You don’t need to write any additional code. All functionalities are already implemented.

Entire Code

Entire Code Example

Conclusion

That’s it! With minimum code, you can add simple CRUD operations. You don’t need to write any complex code to handle CRUD!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!