如何在运行时从UITableView添加或删除行?

时间:2021-12-25 07:21:03

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!

我确信这是一个非常令人讨厌的问题,但我无法弄清楚。我有一个UITextField * numberOfPointsTextBox,它允许我的用户输入一个整数。我希望这个值是UITableView * myTableView中与numberOfPointsTextBox在同一视图中的行数。我还希望用户能够更改此值并相应地更新单元格数。我能找到的唯一解决方案是创建一个“for”循环,为每个循环创建一个NSIndexPath,但必须有一个更好的解决方案。有任何想法吗?提前致谢!

3 个解决方案

#1


1  

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.

UITableView显示的行数通过UITableViewDataSource的tableView:numberOfRowsInSection:方法控制。您的代码应该监听UITextField * numberOfPointsTextBox的更改,更新您已配置UITableView的UITableViewDataSource的自定义实例中的点数,最后在UITableView上调用reloadData以完成更新。

#2


0  

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

您只需要更改numberOfRowsInSection中返回的值,这将导致表只呈现那么多行。

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.

因此,当numberOfPoints文本框的值发生更改时,请在tableView上调用reloadData,然后使用numberOfRowsInSection中文本框中的值。

#3


0  

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

您的tableview数据源必须是可配置的,因此您可以在用户在文本字段中输入一些值后设置项目数。之后,您可以只调用UITableView实例的-reloadData方法,这将强制表视图重新查询数据源中的所有项

#1


1  

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.

UITableView显示的行数通过UITableViewDataSource的tableView:numberOfRowsInSection:方法控制。您的代码应该监听UITextField * numberOfPointsTextBox的更改,更新您已配置UITableView的UITableViewDataSource的自定义实例中的点数,最后在UITableView上调用reloadData以完成更新。

#2


0  

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

您只需要更改numberOfRowsInSection中返回的值,这将导致表只呈现那么多行。

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.

因此,当numberOfPoints文本框的值发生更改时,请在tableView上调用reloadData,然后使用numberOfRowsInSection中文本框中的值。

#3


0  

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

您的tableview数据源必须是可配置的,因此您可以在用户在文本字段中输入一些值后设置项目数。之后,您可以只调用UITableView实例的-reloadData方法,这将强制表视图重新查询数据源中的所有项