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.
Related
How do add button on each cell for table view cell so that it know which one is from.
Example:
Data have 2 products which is product A and product B, then I store them in array which is [product]. Inside the array, there have their detail which are name and price. So, I have successful making these into the table view.
The Question is how to insert button into the table view cell?
Example:
When button pressed, it know which row is pressed which is when press the 2nd row will be product B.
I'm newbie here. Need help, thanks.
You have two options:
Option one - easy, quick, bad:
create instance of the UIButton with the frame to position where you actually want the button to be placed. add tag property as well as the action. Action would be the same for the each button and you could use that tag to do the different actions.
Option two:
Create subclass of your UITableViewCell. Add all the element you want there. Create action for the button and you can set the target UITableViewCell or UITableViewController. If you choose UITableViewCell to be the target you will have to implement delegate, confront that delegate in the UITableViewController and implement that delegate function there. If you choose controller to be the target, you will have to send it to the cell. You will have to set tag as well.
I would use option two because it is scalable, easly maintained, and you have visual of the cell. Adding more components is easier as well.
I don't thing you need to put button or any other control in UITableViewCell to identify the selected cell.
All you need to do, is to implement following delegate method in to your class where you have implemented UITableView
tableView(_:willSelectRowAtIndexPath:)
tableView(_:didSelectRowAtIndexPath:)
and these methods will let you know selected UITableViewCell indexPath and you can do what you want to do on selection.
Note: do not forget to set your UIViewController as delegate to UITableView (almost like you did in case of DataSource).
Here's a Github repo you can refer, the code is not in good shape though.
I am asking this question as I didn't get any proper/relevant answer
(means I got the answer but not in the form that I want).
Hope anyone of you can provide me the right solution.
OK , My Question is about editable labels.
I have one table view that contains customized cell(s) with 6 different labels in each cell.
What I Want :
1) I want to edit the label when user touches it (Changes will be
saved).
2) When some save button is pressed, I want all the values from the
table view to be saved in an array.
I am attaching screenshot herewith (Hope it may help you to understand my question).
Note :
In the above screenshot what i want is when user touches the "Cotton" word, It can be editable and when user touches the next word (suppose "Pallet") , change in the first word (means in "Cotton") must be saved. There will be one button called "Save"(not in screenshot). When user presses "Save" button , all the values of Tableview should be saved in some array.
It will be very helpful if you can provide me the answer with relevant code.
First of all, in this case, it is best to forget about using labels and use UITextField instead. If you implement UITextFieldDelegate's methods, they will let you know when one starts editing or finishes.
So here is what I would do. Create a subclass of UITableViewCell and make it look exactly how you want it to. Also, it should also have an NSIndexPath property, so you will always know exactly which row gets edited. (if your cells look anything like those in the screen-shots you provided, this step should be rather easy)
Also, it should implement the UITextFieldDelegate protocol and also create one that will let you know when a cell has been edited so you can save it's contents.
In your controller, you should create a temporary array that will be a copy of you data source. When a cell gets edited, you should update the info in your temporary array. On pressing save, you should save this array and make it the data source. Else, just set it to nil and ARC will take care of it.
I can't provide you with code for now, but it shouldn't be hard to code it once you understand the logic.
You have to subclass UItextField so that they can store rowNumber and productName.
Change the UITextField appearance when user taps on it you can catch the touch event on textFieldDidBeginEditing,
And save your Data for a particular row in a dictionary then this dictionary could be saved in an array pointing to the rows of the table .
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 !
:-)
I have to create a UITableView. Suppose there are 2 table views TableView1 and TableView2. TableView2 will contain suppose 2 text boxes. And I have to add TableView2 in one cell of TableView1. Finally I have to access the values of text box from the inner TableView2 in the TableView1.
I am new to iPhone development. In short I have to add a tableview to the cell of another tableview. I don't know whether this is possible. If it is, would someone please provide me some link or code to do it.
This is not look good design. You can also use two text field in same cell and make it more in height also make a horizontal separator. also if you have only two textfields then make them global and declared as property.And add in a particular cell.
Read some tutorial for making custom cells and give some time to learning these tutorials.
See this link and by googling you can easily find more on this topic.
Yes, you can do. Of course, It is not good practice to do like this. Still you want then here is the trick to do that.
Create Two ViewController and inherit them using UITableviewController say ViewController1 and ViewController2.
Now in cell of Tableview1 add ViewController2 object. So Tableview2 would in Tableview1's cell. Now you need to handle inner tableview delegate & Datasource in Viewcontroller2. So you don't need to do lots of if.. else in same view controller.
Now you can take two textfield in Viewcontroller2.h file and assign cell's textField to that in cellForrowIndexPath method. So you can access both the textfield any time you want.
I know my answer is bit confusing but ask me any question if you have.
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.