Deleting Rows


TableView: Landmark Book
7m 54s

The course is part of this learning path

Start course

This course is going to explore table views, which are views that allow you to display data in your arrays. Then we're going to put this knowledge into practice by building an app called Landmark Book.

Intended Audience

This course is intended for beginners who want to learn how to build apps for iOS.


To the most out of this course, you should have some basic understanding of programming and computer science in general.


Hi. Within this lecture, we're going to add a delete functionality in our table views as well. So, maybe you have come across apps that lets you swipe left and delete the current data in the table view. So, that's what we're going to learn within this lecture. So, remember we have assigned this to be a UITableview delegate, so we can reach some functions related to this UITableview. And editingStyle, commit editingStyle is one of them. So, choose this commit editingStyle function and we're going to define an editingStyle in here and we're going to say what to do when this happens. In order to do that, we're going to control this editingStyle over here. So, I'm going to say if editingStyle and as you can see this is a UITableview editingStyle. Okay. And we have some different kinds of editingStyle. Actually if you say == and hit 'dot' you can see delete, insert or none. So, insert is for inserting some new data. So, this indicates a green circle on the right hand side, but we're not going to go for that. We just want to learn how to delete it.

Okay, so this is kind of red when we swipe left. So, I'm going to go for if editingStyle is actually delete, then I'm going to go and remove that related row from our current view. And how do we do that? Since we can do landmarkNames.append or landmarkImages.append, we can do the opposite as well. We can say landmarkNames.remove, and we can remove a data from an array using that method. So, if you go over here and say landmarkNames.remove, it will ask you for an integer. And this integer actually specifies the index that we want to remove the value from. So, for example, if I want to remove the colosseum, I can just say remove(at: 0), okay. And how can I get this index? Of course I can say indexPath, that role because this is given to me in the function. And I'm going to do that for landmark images as well not to break the synchronization of these two landmark names and landmark images arrays. So that's how you delete the values from arrays. But also we have to delete them from the table view itself as well.

So, if you say tableView, you can just go for tableView.deleteRows, and will ask you which row to delete, right? So, we have the index path. So, it's it's asking for an index path and we have this here. So, if you just hit 'Enter', you can delete this, and you can replace it with indexPath. So, this is the small one that is given us within the function, and it says that do we do with animation. Okay. So, you can hit 'Enter' to hit 'dot' and you can say why not? You can do a fade. So, it will delete the role with an animation of fading. So, that's all. I believe we can try this right now. So, let's open our app and let's swipe left a little bit. And as you can see, we can delete all these values anytime we want. So, it works. And if we just run this one more time, of course we're going to see all the values, and it doesn't make sense to have this delete functionality in an app like this because we don't start those values in a database. We just write them in our code. If we were to store those values in a cloud database, which we will in the following sections by the way, then it would make much more sense because we could have deleted them-

the cloud firebase or cloud database as well. But right now we know how to make this functionality. So, in the feature if we are working with a cloud database, we can delete them from the arrays. We can delete them from the table view and we can delete them from the database as well and they won't be back. So, this is very useful to use in your apps. So, we're going to stop here. And we're going to continue making this clickable in the next lecture.

About the Author
Learning Paths

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics