Swift - How to create UITableView programmatically, insert and delete rows with animation

preview_player
Показать описание
In this tutorial, we will look at how to easily create UITableView programmatically using UITableViewController as our root view controller. We then register our cell and header classes in code, returning them with our UITableViewDatasource and UITableViewDelegate methods.

Finally, we move onto how to perform cell deletion and insertion with animation using insertRowsAtIndexPaths and deleteRowsAtIndexPaths.

Completed project:
Рекомендации по теме
Комментарии
Автор

7 years later, this video still helps me a great deal with my app! Clean and clear code as always))

antonlebedev
Автор

Wow, excellent. I am a beginner on this. But your code is flexible to change "Button Names" like "Delete" to "Borrar" in Spanish. You do all of this programmatically, and that's great to learn how Xcode Interface works. It helps to Spanish programmer to manipulate the User Interface. Most of the code that Xcode did, it is in English. Good Job man, continue with this great videos.

Pocho_DNA_Cod_
Автор

i love you - FYI for anyone that can't get the action buttons to respond, declare the buttons with "lazy var" instead of "let"

khgriffi
Автор

I've been watching a lot of video tutorials for Swift over the last 2 years. I really like your approach and think it is probably the best I've seen. Awesome job! Subscribed and will be checking out the rest of your videos!!!

timjohnson
Автор

Great video. It's nice to see a more programmatic approach. Not enough people take that approach. I'm going to subscribe and check out your other videos. Thanks mate!

sdoctor
Автор

great vid Brian. I think this actually solves the other issue I mentioned to you on your website. Just need to modify the array to use my Firebase data, then add edit actions on tap

matthewjennings
Автор

Wow... Awesome Video... I think this is way above my skill level but I am going to give it a try.

On one of the View Controllers in my app I am trying to have the top section with 3 text fields and a Save button. Under that I want a table view that displays the information the user entered.

Is there away to get more than one text field using the main.storyboard or will I need to trash it and try it this way?

James-pjcg
Автор

Great video! If I wanted the animation portion to run whenever I bring up the tableVC, can I pull the animation portion out of that function you have and make it's own function? Say if I had 2 VC's and the second was the tableView. every time I go to the tableVC, the animation runs. Kinda gimmicky, but something I'd like to try =) Thanks!

michaelstram
Автор

Hi, thank you for this awesome video. But I have one question. I've seen some ISO tutorials, and in these tutorials, they drag table view controller on storyboard. So I am wondering what's the difference between dragging and writing code directly? Which way is better? Like after you wrote these code for table cells, will Xcode generate a storyboard based on your code? Thanks for your time.

shuyangyu
Автор

I have a question. If we delete item 2, how we can write the animation code to change item 3 to item 2 without scrolling or refreshing

nayanakf
Автор

Hi Brian.How are you?How can I iterate through an Array inside a tableviewcell class? is there a indexPath on tableViewCell classes or do I need to move my array to my tableview class? thank you

ecptavares
Автор

if i wanted to create a custom fixed header above the table what should i do? please let me know the steps for the same

deeptichaudhari
Автор

Hi thanks for this video, it got me started, Any hints on how to add additional sections or headers to the UItableviewview? Thanks

functionalvitals
Автор

I've seen you use different types of constraints in your other videos. What made you use the visual format and not the anchor format in this video?

nickeckert
Автор

I have a question, I have a table cell that contains 2 UITextViews with dynamic height and a UILabel, I did set: tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140
I have number of rows set to return 1 but tableview still displays 10 and the row doesn't increase in size, it's short. I have set constraints with anchors. Any ideas?

MichaelGotiashvili
Автор

Can all these classes (Header, MyCell) be put on separte files and still be able to communicate with each other ?

jachuonbass
Автор

Unrelated to this video, how do I navigate to a different view (via navigation controller) programmatically. I am using SWRevealViewController and would like the buttons to navigate to a different view with the navigation back shown. I think the essence of this request is just how to use navigation controller programmatically.

StephenRayner
Автор

Hey bro. Super nice! Curious to why are these buttons and labels computed properties when you create them every time you make a cell anyways?

doktoren
Автор

Is it possible to add a cell right in the middle of your tableview?

Also, I've another question wrt to the process of inserting a new cell.
When you added a new item to your collection array. Then the callback methods
should be invoked in order to populate the table view again.
So in other words, the method tableView(_:cellForRowAt:) is called and the tables gets refilled, with an additional cell.

When I try to do this, my Xcode crashes with: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (5) must be equal to the number of sections contained in the table view before the update (4), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

Via print outs I can see that the crash happens before tableView(_:cellForRowAt:) is called again. So there is no chance that the new data source is even tried to be used for refilling the cells.

Any ideas?

condemned
Автор

hey Brain, its really helpful tutorial by u. thank you for sharing ur knowledge. i am going to ask question about expandable table cells. how can i create expanded table ? thanks beforehand

sardoribroximov
join shbcf.ru