简体   繁体   中英

Add new tableview cells dynamically

I have seen lots of posts about adding and deleting the cells in the tableview. What i want is when user clicks on add button in the cell it must ask user what to enter the text in that cell,then user enters the text in the cell and then it is added in the tableview. Can anyone tell me how to accomplish this?

I would put the add button in the footer or header, rather than in a cell, it is my personal opinion that managing the adding of cells would be easier.

Apart from this, don't know exactly what your problem is, if you make your controller as uitable delegate then you can easily add or remove cells by accessing all the relevant methods, in particular that one:

- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleInsert) {
       // ......
    }
}

which is called by:

[yourTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath ] withRowAnimation:UITableViewRowAnimationAutomatic];

Have a method like (UITableViewCell *) createNewCellForTableView . This will create and return UITableViewCell.

In your CellForRowAtIndexPath use this method for creating all cells for your tableView . This needs to be done this way because when you perform a [tableView reloadData]; it needs to create new cells each time you add a new cell.

Finally after you create new cells, in the IBAction of the ADD button, make sure you reload the tableView.

~ A simpler alternative:

Since you are just looking for the user to add text, you can have a textField embedded in the uitableViewcell. As soon as the user finishes typing, you can have the text added to the dataSource (the array populating the tableView ) and reload the tableView. Make sure you have the textField enabled or disabled as you want it to be.

This is how I do it and it works:

first, create an instance variable as follows:

int numRows = 0;

Implement the method numberOfRowsInSection as follows:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return numRows;
}

Now in my application, at one place, I'm using a REST API and getting a JSON, i'm updating the value of numRows as numRows = [jsondata count];

Write your method cellForRowAtIndexPath as you would want it to be if you had the dynamic content. For example in my case, I'm using the indexPath.row and preparing the cell and returning it.

This method will only be call when the variable numRows is greater than 0. So as soon as you have your dynamic content, update the content of numRows as I explained above and immediately call the method [YourtableView reloadData];


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM