Garrett
Garrett

Reputation: 5630

How do I add or remove rows from UITableView at runtime?

I'm sure this a very nooby question, but I can't figure it out. I have a UITextField *numberOfPointsTextBox which allows my users to input an integer. I want this value to be the number of rows in the UITableView *myTableView which is in the same view as the numberOfPointsTextBox. I also want the user to be able to change this value and have the number of cells update accordingly. The only solution that I have been able to find is to create a "for" loop which would create an NSIndexPath for each, but there must be a better solution out there. Any ideas? Thanks in advance!

Upvotes: 2

Views: 866

Answers (3)

Denis
Denis

Reputation: 6413

Your tableview datasource must be configurable, so you can set up number of items after user has entered some value in text field. After that you can just call -reloadData method of your UITableView instance, that will force table view to re-query all items from datasource

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726987

The number of rows the UITableView displays is controlled through UITableViewDataSource's tableView:numberOfRowsInSection: method. Your code should listen to changes of UITextField *numberOfPointsTextBox, update the number of points in your custom instance of the UITableViewDataSource with which you have configured your UITableView, and finally call reloadData on the UITableView to complete the update.

Upvotes: 1

Nick Bull
Nick Bull

Reputation: 4286

You just need to change the value returned in your numberOfRowsInSection which will then cause the table to only render that many rows.

So, when your numberOfPoints text box has a change in value, call a reloadData on your tableView and then use the value from your text box in numberOfRowsInSection.

Upvotes: 0

Related Questions