i have an array of tableViewCell. each tableviewcell contains the textfield. and all of these are in the detail view. if I enter a value in the textfield and tap the back button, and when i return back to the detailview , the value in the textbox disappears.
please someone help me out for how to store the values.
Thank you in advance
A cell gets recreated when it is shown again. If you have a value which the user changes you will need to store that in an array which the cells must read from when they are created in the cellForRowAtIndexpath delegate method.
You could use the uitextfield delegate method shouldChangeCharactersInRange to store the text that is written in an array which the cells can read from when assigning values to the text property of their textfields
When you press the back button, the view that contained it is deallocated, so you will have to store that data somewhere outside of it. Two of the most common solutions to store shared data in the app are either storing it in the App Delegate or having a Singleton class.
You can just store the value of the textField in the AppDelegate method, and then store it back to the UITableViewCell by implementing the viewWillAppear method with tableView reloadData.
You need to carefully assign the value of that UITableViewCell so that the saved value corresponds to the particular tableViewCell. I would say that you use tags or may be subclass UITableViewCell an embed a UITextField inside of it. These are just options - you can logically implement this in your favorite way too !
:-)
Related
I am new at iOS development and i have some issue of cellForRowAtIndexPath method of UITableView.
In my application i have multiple rows in UITableView. when i want to see each row then obviously i need to scroll UITableView.also in UITableView i added UITextField to each UITableViewCell . when i add value
of UITextField in DataBase then its work properly but when i want to edit values of UITextField and i add values of TextField in DataBase then problem is created.
PROBLEM :- If i want to add all editable values of TextField in DataBase then i must need to be scroll UITableView up to down. if i do not scroll then invisible TextField is not add in DataBase :( Here in my Application add and editing are in same UIViewController base on ID that i passed.
by Googling i also found that cellForRowAtIndexPath is not called for all rows. It is only called for visible rows and then called for additional rows as the user scrolls to them. Old rows are also destroyed as they go off the screen and it will request the cells again if the user scrolls back up.
so, please help me how can i solve my problem,
EDIT FOR MORE EASY TO UNDERSTAND MY PROBLEM :
For Example :
I have two UIViewController.
1 - FirstViewController
2 - SecondViewController
in FirstViewController display all list of Name in UITableView and it has two choice either select name in UITableView or add new data by tapp BarButton from UINavigationBar .This both processed adding and editing are done in same ViewController called SecondViewController . data (or values) Add by SecondViewController is working properly
But when i select any row ( or name ) from FirstViewController then related ID pass to SecondViewController and all information are display on TextField base on ID. here all values are get from DATABASE and i store it to NSMutableDictionary and then it store on textField by access KEY of Dictionary. but values are store in TextField which are visible. invisible TextField not store values even i get from DATABASE . so i must need to first scroll UITableView.
Thanks in advance :)
Then implement like given below
Get the database value and store it in NSMutableDictionary and accessible through out by declaring in interface or .h file.
Then when each time your tableView or textField loads read the values from dictionary and display.
And in your textFieldDidEnd or textFieldDidChange delegate get the data from textField & save/store back to your dictionary with the same key. This time it just replaces the old value as you know.
At last just save all the dictionary values into your database on backAction or viewWillDisappear method.
This should work for you, if not let me know.
I've got a UITextView inside a UITableViewCell subclass. I have no problem getting the new height of the Text view and cell. The problem I have is telling the UITableView to update.
I have implemented heightForRowAtIndexPath: to return the live height of the cell as the TextView expands.
But somewhere `[tableView beginUpdates]; [tableView endUpdates]; must be called.
How? Should I add a delegate property to the UITableViewCell which is set to the UITableViewController subclass? And then send a delegate message when the cell expands height and the Tableview needs to update? It seems a little weird to have a delegate between the UITableViewCell and Controller?
I tried using NSNotificationCenter, but I have more than one editable cell, and more than tableview of this nature. So there is no way to register only for notifications for the cells without copying and pasting the same line over again, which isn't nice (as the cells are created in IB, and are not in an array or set), and having multiple tableviews means an exception occurs on the other table view as it is told to update but nothing changes.
I've seen lots of questions and answers on this topic, but when it comes to updating the tableview they all just say "now update the tableview" and not how to. So how do I telly he tableview to update, from one of it's cells?
I would think that this behavior would be best implemented in the UITableViewController instead of the view itself (the UITableViewCell).
Your controller is responsible for setting cell height, and typically will be the delegate for your UITextView's, so let it handle all of this.
In your textViewDidChange method, figure out what the new height of your cell should be, update your data structure to reflect that, and call reloadRowsAtIndexPaths:withRowAnimation: to have it actually change.
Edit:
So since you didn't like my first suggestion, another way to do this would be to add a recommendedRowHeight property to your custom UITableViewCell.
Then, you can either observe this property from your UITableViewController or implement a delegate protocol with a method along the lines of:
- (void)recommendedRowHeightDidChange
// or
- (void)recommendedRowHeightDidChangeTo:(CGFloat)newHeight
Then, when your height changes, update your recommendedRowHeight property and call your delegate's method if you go that route.
Either way, once your controller figures out that the recommended row height of a cell has changed, it can do what it is supposed to do. Update your data structures reflecting the current row heights and then call reloadRowsAtIndexPaths:withRowAnimation:.
You can add your tableview controller object as a weak reference to your tableview cell class. And in tableview controller you can have a method which will be called from tableview cell class.
So I don't know what the best way to follow MVC is. Similar to the address books app, I want to have a UITableVeiewcell that has the ability to edit notes. I figured I would do that with a UITextView in a UITableViewCell subclass. My subclass has just that as a property, and a label that says "notes". I can see a few use cases that I need to consider,
1) when they are done editing and click outside or hit return.
2) when the text goes beyond the size of the cell I need to resize the cell.
Because my UITextView is in IB, is there a good way to define the delegate methods for the UITextView since my UITableView is in another ViewController subclass? Like how do I pass that information back?
Or, is it better to create my UITableViewCell subclass in code since it's just a couple of items so all my delegate and resizing code is done in the view controller class?
Thanks!
After text field editing was finished, you can store it's value in some dictionary in your controller. You can use cell's indexPath as key in this dictionary. In such way you will not lose your data with dequeue cells.
To resize cell you must call reloadData method and change rowHeight property of entire tableView or implement tableView:heightForRowAtIndexPath: delegate method to set needed row height to current cell.
I haven't use UIKit since iOS 3.1, so the second part of my answer can be out of date, but I hope it will help you =)
How to get the selection cell still in the selected state in uitableview. Actually I am pushing a view from a tableview when came back then the selection cell is deseleted. I want to be show the previous selection cell when came back also how to do that.
In didselectrowatindexpath use the method selectRowAtIndexPath:animated:scrollPosition: and the row stays selected.
when you are going and coming back from anotherview make sure that save the selectedCell and then in viewwillappear method reloaddata.In cellforindexpath write the code of selection style uitableviewcellselectionstyleblue.
first, you can store the selected indexPath in user default in the didSelectRowAtIndexPath method.(maybe you can store a indexPath that is bigger than your table view's row count to indicate initialization stage, which means that no row has ever been selected)
then, you can load the stored indexPath in the viewDidLoad method using
tableView selectRowAtIndexPath:<#(NSIndexPath *)#> animated:<#(BOOL)#> scrollPosition:<#(UITableViewScrollPosition)#>
meanwhile, you can do some check (like mentioned above or something else) to check if there's nothing selected yet.(saving a bool in user defaults works, too)
Guys, I didn't find a clean and a simple solution for the following issue. I've a UITableViewController view which uses a UITextFieldCustomCell that I implemented.
The table has several rows, that requires the user to scroll down an enter values on each cell, which contains a UILabel and a UITextField.
Every time the user change the value on the UITextField the UIViewController gets notified and stores the value in a NSDictionary using the cell indexPath.row property, in order to identify what's the key for the cell where the value needs to be stored.
The problem is if the user keep focus on a cell and then scrolls up or down (removing the cell from the view) makes me unable to get the indexPath for the cell, since it's not visible.
So, I cannot store the value since I don't know from which cell the value is coming.
Have anyone run through this issue before?. It seems to be a common design between iPhone applications, does anyone have an idea if is this a good implementation or not?
Thanks!
I assume you're observing textFieldDidEndEditing:. Are you saying it's not firing, or that it is firing, but it's no longer in any of the cells (possibly because it's in the process of being removed)?
Assuming the latter, my approach would be to use setTag: on the UITextField to make it easy to keep track of its index. This would save you from ever hunting around in your cells, even in the case that they are on the screen.