how to save the content of a uitextview to a tableview - ios

I have a UITextView and when I press a 'Save' button I would like that the text will get added to a row in my UITableView. I've set up the table view already. I'd also like to get to the UITextView when selecting one on the table view.

First, you need to add the data to your data source. Depending on how your table is set up that might just entail adding an extra string to an array, if you edit your question with more detail I can be more specific here.
Next you call insertRowsAtIndexPaths:withRowAnimation: on your table view, changing the value of the index path depending on where you've added the new data.
[tableView insertRowsAtIndexPaths:#[NSIndexPath indexPathForRow:0 inSection:0] withRowAnimation:UITableViewRowAnimationAutomatic];
The table view will take care of the rest, it will call cellForRowAtIndexPath: to make the new table cell (using the data you just added to the data source) and will even animate it into position for you.
For the second part of your question, you'll need to implement tableView:didSelectRowAtIndexPath: and in there get the appropriate string from your data source based on the selected row, and set the text view accordingly. Something like
textView.text = myDataSourceArray[indexPath.row];

Very basic, but this example should do the trick.
https://cloud.jakota.de/public.php?service=files&t=922500d0732da5ce3cc71b6fdf517a83

Related

iOS: Adding row to tableview

I have a tableview that is based on a array of DB results, these results contains a date field. I have a custom cell that contains an image and labels. I'm trying to do:
At cellForRowAtIndexPath I verify if the date of current item (objectAtIndex:indexPath.row) has date field bigger than the last item (objectAtIndex:indexPath.row-1). If this is true: I want to add a cell filling the ImageView with a certain image, but I need to add a new row just for show this image.
How can I do this? I'm already doing this verification, but I need to add a new cell...
Do not use the cellForRowAtIndexPath to decide how many cells you want to have. At the point this method is called you should have already setup the data source to provide table view with all information needed.
Here is what you need to do. Refactor your code in a way so you:
Setup the data source first.
Force reload of the table view either by calling the reloadData method.
hey you can add the object in your data base(for example ns array) and refresh the table view with method
[tableView reloadData];
then the method cell for row at index path will be called again and it will refresh the table view's items.just make sure the method cellforrawantindexpath in your code knows to handle the new data type(make validations).
Your tableView data source should not contain any of that logic where the content of once cell depends on the content of another cell. Instead, you should have a data item for each requested indexPath and that data item should contain ALL logic necessary for the cell to be configured. If an action on that cell has an effect on how another cell should look, you apply a change to the corresponding data-item, and then call reloadRowsAtIndexPaths: for the indexPaths.
In short: configure cells ONLY in tableView:cellForRowAtIndexPath: or tableView:willDisplayCellAtIndexPath, and ONLY do configuring. Other logic should be placed in some data(-controller) object.
As suggested, you should add an item to your data-array. Then call -insertRowAtIndexPath: on the tableView. ReloadData is like a big ugly hammer that you only use when ALL of the data for that tableView changes.

Updating UITableView cells according to the text entered in a text filed that is in the same main view

Is it possible to update the cells of a table according to the value entered in the text field which is in the same view or when clicking on a button which is in the same view. can this be done when all the components are in the same view? of course i know that i will have to use a cell and a table view an all in that view.
If it's possible please hint me to what method in the TableView Delegate I should work with to make this happen or if I have to write a custom method.
Thanks in advance.
You should look into UISearchController
Links to explore:
http://www.raywenderlich.com/16873/how-to-add-search-into-a-table-view
http://jduff.github.io/2010/03/01/building-a-searchview-with-uisearchbar-and-uitableview/
http://www.iphonedeveloperdiary.com/2010/07/adding-a-uisearchbar-to-top-part-tableview/
http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/38913-slicks-uitableview-series.html
https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UISearchDisplayController_Class/Reference/Reference.html
OR...
If you want to do it using something like a simple UITextField, then in the -textFieldShouldReturn, you can use NSPredicate to create an array of searched objects and then simply can do a [self.tableView reloadData].
(just ensure that the datasource of your tableView is using this array)
You can do it just by using
[tableView reloadData];
On Clicking if button please ensure to update UITableView Datasource Array to update.

How can I get the value of a UITableViewCell that is not visible?

I have a table view cell that has many rows with a UITextView. In those UITextView the user can enter values.
The problem that I have is that if I want to get the value of a row when the row is hidden I get nil.
NSString *cellValue = ((UITextField *)[[[self.table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]] contentView] viewWithTag:[self.table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]].tag]).text;
I was thinking to save the value that the user enters in the UITextField, but if the user clicks a row that performs the calculate the delegate textFieldDidEndEditing is not being called.
I don't know how can I get this values in a secure way.
You should target the data source that is behind the UITableView. The UITableView is just a display mechanism and not the primary resource for getting to data. You can get the index for the cell based on the title, but then I would look tot he data source to get your actual data. Better yet I would just handle all operations against the data source itself if that is possible. When you build the table initially your data source is a list or array of some kind I am sure so you can just use it. You may need to create a variable on the view and retain the list/array there to make sure you have it and you should be all set.
Good Luck!
As mentioned, you would generally use the UITableView data source to access this.
That said, you are probably not seeing the data you expect because the you are using dequeued cells. If the number of cells is small, you could consider caching your own cells in an NSDictionary whose keys are some combination of the section and row, and accessing their view hierarchy via your own cache.
You can access rows that are not currently displayed by directly referring the tableView's dataSource with the wanted IndexPath.
You can achieve this by doing something like this (when using Swift 3):
UITableViewCell cell = self.tableView(self.tableView, cellForRowAt: indexPath)
What is happening most likely is your reusing cells. So when your cell with the textfield goes off screen it gets reused so you cannot access it anymore. What you will need to do is grab the value out the the textfield right after they enter it and store it somewhere. You can use – textField:shouldChangeCharactersInRange:replacementString: so that when ever the user enters a character into the textfield you can save the string.

How to refresh a UITableView in iphone?

I have a UI table view loading data from network. If the user move the finger on one cell, one button will be displayed, just like iPhone style delete button. If the user click this button, the value of one label in the cell will be changed accordingly. However, I didn't find any way to make table view to re-draw this cell, except for reload the whole table. I don't want to use reload data method, because I just need change the value in one cell instead of the whole table. Any suggestion on this?
Take a look at the method
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
Calling this method will result in your table view's data source asking its delegate for information about that cell.
For future reference, I found this very easily by checking the documentation on UITableView. I suggest you do that in the future before posting here.

Saving data from custom UITableViewCell

I have an edit screen which is comprised of one huge UITableView.
Each cell in the table is a custom UITableViewCell comprising of a UITextField
I am able to load up the table, scroll around and enter information. However, during scrolling the table cells lose the data entered and I understand why this happens.
I realize I need to save the data as soon as the user finishes entering the data using the textFieldDidEndEditing method.
Question: I want to store the data entered in a custom object that is available in the ViewController - how do I access this from the UITableViewCell? Also, how do I identify the row and position of the table cell so that based on the position I can populate a specific attribute in the object
Question: Am I doing this the right way? Or is there a better way to do this?
As you are mentioning only a single text field, we can make do with an NSMutableArray object containing strings for each cell. You will have to fill the array with empty strings and use this info to fill the text field. It will be something like,
textField.text = [textFieldStrings objectAtIndex:indexPath.row];
Since you're doing it programmatically within the view controller, you will have to set the view controller as your text view delegate. You can do
- (void)textFieldDidEndEditing:(UITextField *)textField {
UITableViewCell *cell = (UITableViewCell*)textField.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
... Since you've the index path, you can map it to an array.
[textFieldStrings replaceObjectAtIndexPath:indexPath withObject:textField.text];
}
This would be the basic idea. Start with a mutable array of empty strings and modify them every time the text field is updated by replacing the original text with the new text. Hope this helps.

Resources